# Simulates 100 coin tosses, finds longest run of heads, repeats 10000 times, examines distribution of longest runs # # modify this program for different/general values of n (number of tosses), N (number of repetitions) # x=c("H","T") # # initialize vector of longruns longruns = rep(NA, times = 10000) # # begin loop across number of repetitions for (j in 1:10000) { # # initialize longrun and current run longrun = 0 run=0 # # generate 100 random coin tosses y=sample(x,100,replace=TRUE) # # now start the loop that goes through the coin tosses to calculate runs of heads for (i in 1:100) { # # if current toss is H, increment run by one if (y[i] == "H") {run = run + 1} else {run = 0} # # if current run exceeds longrun, replace longrun with current run if (run > longrun) longrun = run } # # after all 100 coin tosses are analyzed, record longest run for this repetition longruns[j] = longrun } results=table(longruns); results mean(longruns) hist(longruns) long5 = (longruns > 5) table(long5) prob = sum(long5/10000); prob