ntrials = 100000 res = rep(0,ntrials) tot = 0 for (j in 1:ntrials) { nflips = 50 trial = rbinom(nflips, 1, .5) maxRun = max(rle(trial)$lengths) tot = tot+(maxRun >= 8) } m = tot/ntrials cat(m)