#===================================================== # Simulation of the probability distribution of success # count. #===================================================== #----------------------------------------------------- # 1. Function to generate sequence of successes and # failures. Arguments: number of trials is n, success # probability on any given trial is p. Function returns # vector x of 0's and 1's. #----------------------------------------------------- outcomes=function(n,p) { u=runif(n) x=1*(u<=p) return(x) } #----------------------------------------------------- # 2. Simulate n trials num.sets times. # Store counts of successes in hits. #----------------------------------------------------- n=30 p=.26 num.sets=10000 hits=numeric(num.sets) for (i in 1:num.sets) { hits[i]=sum(outcomes(n,p)) } #----------------------------------------------------- # 3. Plot histogram of hits. #----------------------------------------------------- bounds=(0:31)-.5 hist(hits,breaks=bounds,freq=FALSE)