#6c nflips = 50 ntrials = 10000 total = 0 # We’ll keep a running total of all the trials’ longest runs for (j in 1:ntrials) { # One trial consists of 50 flips trial = rbinom(nflips, 1, .5) # binomial(1,.5) = bernoulli(.5) # rle() finds the lengths of all the runs in trials. We add the max to total total = total + max(rle(trial)$lengths) } # The average maximum run is the total/ntrials aveMax = total/ntrials aveMax #6d nflips = 50 ntrials = 10000 # We’ll keep a running cnt of all the trials with a run of 8 or more count = 0 for (j in 1:ntrials) { trial = rbinom(nflips, 1, .5) # binomial(1,.5) = bernoulli(.5) count = count + (max(rle(trial)$lengths) >= 8) } # The probability of a run of 8 or more is count/ntrials prob8 = count/ntrials prob8