Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 44
#simulating a single coin flip #let 1 be "heads" and 0 be "tails" randint(0,1)
1
flips = [] flips.append(7)
flips
[7]
flips.append(9) flips
[7, 9]
#repeat n times n = 10 flips = [] for i in [1..n]: flips.append(randint(0,1))
flips
[1, 1, 1, 0, 0, 1, 0, 0, 0, 1]
L = [4,5,6] L[1:]
[5, 6]
#repeat n times n = 100 flips = [] for i in [1..n]: flips.append(randint(0,1)) flips
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0]
#longest keeps track of the longest streak so far longest = 1 #current keeps track of the length of the current streak current = 1 #last keeps track of whether the last flip is 0 or 1 last = flips[0] for x in flips[1:]: if x == last: current = current + 1 else: last = x if current > longest: longest = current current = 1 print "The length of the longest streak of heads or tails is {}".format(longest)
The length of the longest streak of heads or tails is 6
flips
[0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0]
#store our results in a list length_data #length_data[i] = the number of experiments where the length of the longest streak is i length_data = [0]*101 n=10000 #repeat the 100 flips experiment n times for i in [1..n]: #let's generate a sequence of 100 coin flips flips = [] for i in [1..100]: flips.append(randint(0,1)) #longest keeps track of the longest streak so far longest = 1 #current keeps track of the length of the current streak current = 1 #last keeps track of whether the last flip is 0 or 1 last = flips[0] #flips[1:] is the sequence of flips starting at index 1 from the original sequence of flips for x in flips[1:]: if x == last: current = current + 1 else: last = x if current > longest: longest = current current = 1 length_data[longest] = length_data[longest] + 1 #print "The length of the longest streak of heads or tails is {}".format(l length_distribution = [length_data[x]/n for x in [0..100]]
length_data
length_distribution
[0, 0, 0, 3/10000, 77/2500, 867/5000, 1307/5000, 2179/10000, 92/625, 811/10000, 57/1250, 211/10000, 107/10000, 13/2500, 23/10000, 1/625, 1/2000, 1/2000, 1/10000, 1/10000, 1/10000, 0, 1/10000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bar_chart(length_distribution[:20])
length_data[longest] = length_data[longest] + 1
The length of the longest streak of heads or tails is 6 The length of the longest streak of heads or tails is 8 The length of the longest streak of heads or tails is 8 The length of the longest streak of heads or tails is 5 The length of the longest streak of heads or tails is 7 The length of the longest streak of heads or tails is 7 The length of the longest streak of heads or tails is 8 The length of the longest streak of heads or tails is 10 The length of the longest streak of heads or tails is 7 The length of the longest streak of heads or tails is 7
length_data
[0, 0, 0, 0, 29, 180, 277, 211, 144, 75, 38, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 6, 2, 3, 1, 2, 0, 1, 6, 2, 3, 0, 0 2,, 0]
scatter_plot([(1,5),(2,7),(3,1)])
scatter_plot([(x,length_data[x]) for x in [0..100]])
scatter_plot([(x,length_data[x]) for x in [0..20]])
bar_chart(length_data)
n
1000
n = 200 flips = [] for i in [1..n]: flips.append(randint(0,1))#longest keeps track of the longest streak so far longest = 1 #current keeps track of the length of the current streak current = 1 #last keeps track of whether the last flip is 0 or 1 last = flips[0] for x in flips[1:]: if x == last: current = current + 1 else: last = x if current > longest: longest = current current = 1 print "The length of the longest streak of heads or tails is {}".format(longest)
The length of the longest streak of heads or tails is 6
total = 0 for k in [1..1000]: n = 200 flips = [] for i in [1..n]: flips.append(randint(0,1))#longest keeps track of the longest streak so far longest = 1 #current keeps track of the length of the current streak current = 1 #last keeps track of whether the last flip is 0 or 1 last = flips[0] for x in flips[1:]: if x == last: current = current + 1 else: last = x if current > longest: longest = current current = 1 #print "The length of the longest streak of heads or tails is {}".format(longest) total = total+longest (total/1000).n()
8.06300000000000