#=========================================================== # R program to simulate central limit theorem as applied to # a sum of uniform random variables. #=========================================================== layout(matrix(c(1,2,3,4),2,2,byrow=TRUE)) # Figure with 4 panels # in 2X2 layout. #----------------------------------------------------------- # One uniform random variable simulated 10000 times. #----------------------------------------------------------- n=1 # Number of random variables in sum. repeats=10000 # Number of values to simulate for # histogram. v=runif(n*repeats) # Vector of uniform random variables. w=matrix(v,n,repeats) # Enter v into a matrix (nXrepeats). y=colSums(w) # Sum the columns. hist(y,freq=FALSE,breaks=20,ann=FALSE) # Histogram. title("n = 1") #----------------------------------------------------------- # Sum of two uniform random variables simulated 10000 times. #----------------------------------------------------------- n=2 # Number of random variables in sum. repeats=10000 # Number of values to simulate for # histogram. v=runif(n*repeats) # Vector of uniform random variables. w=matrix(v,n,repeats) # Enter v into a matrix (nXrepeats). y=colSums(w) # Sum the columns. hist(y,freq=FALSE,breaks=20,ann=FALSE) # Histogram. title("n = 2") #----------------------------------------------------------- # Sum of five uniform random variables simulated 10000 times. #----------------------------------------------------------- n=5 # Number of random variables in sum. repeats=10000 # Number of values to simulate for # histogram. v=runif(n*repeats) # Vector of uniform random variables. w=matrix(v,n,repeats) # Enter v into a matrix (nXrepeats). y=colSums(w) # Sum the columns. hist(y,freq=FALSE,breaks=20,ann=FALSE) # Histogram. title("n = 5") #----------------------------------------------------------- # Sum of twenty uniform random variables simulated 10000 times. #----------------------------------------------------------- n=20 # Number of random variables in sum. repeats=10000 # Number of values to simulate for # histogram. v=runif(n*repeats) # Vector of uniform random variables. w=matrix(v,n,repeats) # Enter v into a matrix (nXrepeats). y=colSums(w) # Sum the columns. hist(y,freq=FALSE,breaks=20,ann=FALSE) # Histogram. title("n = 20")