/* Flipping coins probability in Picat. From MarkYourDecisions "Puzzle Inspired By Mark Rober Tweet" https://www.youtube.com/watch?time_continue=3&v=a0rCBKsLoBc """ Mark Rober is one of the most popular YouTubers. Recently, all of his new videos have reached #1 on YouTube’s trending and averaged 20 million views/video. He wanted to put his success in perspective of a statistical experiment about coin flipping–since some success can be attributed to chance. While doing so, he wrote the following: "…if you give 1024 people a coin and give them 10 tries to get as many tails as possible, it’s a statistical certainty that one of them will flip 10 tails in a row…" @Mark Rober On Twitter a few people noticed that Mark made a miscalculation in his example. Mark did acknowledge the issue and some people presented the correct calculations. But such probability problems confuse many people. So I thought it was a great opportunity to cover this kind of calculation. Part a If each of 1024 people flips a coin 10 times, what is chance of at least 1 person getting 10 heads? Part b If each of n people flips a coin 10 times, how large must n be so that there is a 99.9% chance that at least 1 person gets 10 heads? """ Some runs (1024,10): 0 = 4 1 = 9 2 = 28 3 = 139 4 = 238 5 = 247 6 = 186 7 = 111 8 = 48 9 = 11 10 = 3 0 = 1 1 = 6 2 = 38 3 = 110 4 = 214 5 = 259 6 = 200 7 = 137 8 = 48 9 = 10 10 = 1 0 = 1 1 = 8 2 = 38 3 = 124 4 = 218 5 = 255 6 = 230 7 = 96 8 = 43 9 = 11 10 = 0 What is the distribution of the number of 10 runs in the (1024,10) problem 0 = 360 1 = 387 2 = 171 3 = 62 4 = 15 5 = 5 I.e. in 360/1000 times there where 0 10's in 5/1000 times, there where 5 10's. 10_000 runs: 0 = 3628 1 = 3739 2 = 1839 3 = 626 4 = 133 5 = 27 6 = 7 7 = 1 This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => N = 1024, % number of people M = 10, % number of coin tosses _ = random2(), Map = do_tosses(N,M), foreach(Key in Map.keys.sort) println(Key=Map.get(Key)) end, nl. go2 => NumTrials = 10000, N = 1024, M = 10, _ = random2(), Map = new_map(), foreach(_ in 1..NumTrials) T = do_tosses(N,M), NumMax = T.get(M), Map.put(NumMax,Map.get(NumMax,0)+1) end, foreach(Key in Map.keys.sort) println(Key=Map.get(Key)) end, nl. % % N = number of people % M = number of coin tosses % do_tosses(N,M) = Map => Map = new_map([I=0 : I in 0..M]), foreach(_ in 1..N) sum(toss(M)) = Sum, Map.put(Sum,Map.get(Sum)+1) end. toss(M) = [random() mod 2 : _ in 1..M].