中心極限定理(その2)

library(MASS)
library(fitdistrplus)

#Simulation condition
k <- 100 #Number of segment
n <- 1000 #Number of trial

#Random variable generation
#1st distribution
runifmin1 <- 0
runifmax1 <- 1
#2nd distribution
runifmin2 <- 5
runifmax2 <- 6
#Mixture ratio
mratio <- 0.5

s <- x <- y <- u1 <- u2 <-numeric(k)
t <- numeric(n)

for (i in 1:n) {
  for (j in 1:k) {
  x <- runif(1,runifmin1,runifmax1)
  y <- runif(1,runifmin2,runifmax2)
  p <- runif(1,0,1)
  if (p>mratio){
   s[j] <- x
  } else{
   s[j] <- y
  }
 }
 t[i] <- sum(s)
}

u1 <- runif(n,runifmin1,runifmax1)
u2 <- runif(n,runifmin2,runifmax2)

truehist(t, xlim=c(0,6*k), prob=TRUE)
results <- summary(t)
print(results)
me <- mean(t)
st <- sd(t)
x<-seq(0,6*k,0.01*k)
lines(x,dnorm(x,me,st),lwd=2,lty=2,col="blue")