Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monte Carlo simulation of FiveThirtyEight's Riddler problem for February 19, 2016
# http://fivethirtyeight.com/features/will-someone-be-sitting-in-your-seat-on-the-plane/
import random
from collections import defaultdict
n_seats = 100
results_correct_seats=defaultdict(int)
results_by_boarding_order=defaultdict(int)
iterations = 1000000
for loops in range(iterations):
seats = dict()
remaining = range(n_seats)
passengers = range(n_seats)
random.shuffle(passengers)
correct_seats = 0
def pick_random_seat ():
return remaining[random.randint(0, len(remaining) -1)]
for i in range(n_seats):
passenger = passengers[i]
# first passenger on board always picks a random seat
# othe passengers only pick a random seat if there seat is taken
if i == 0 or not passenger in remaining:
seat = pick_random_seat()
else:
seat = passenger
seats[seat] = passenger
remaining.remove(seat)
if seat == passenger:
correct_seats +=1
results_by_boarding_order[i] += 1
results_correct_seats[correct_seats] += 1
print "Distribution of correct seats"
for correct, result in sorted(results_correct_seats.items()):
print "{:3d} - {:.2%}".format(correct, result / float(iterations))
print "Odds that passenger X is in correct seat"
for passenger, result in results_by_boarding_order.iteritems():
print "{:3d} - {:.2%}".format(passenger, result / float(iterations))
Distribution of correct seats
81 - 0.00%
85 - 0.00%
86 - 0.01%
87 - 0.04%
88 - 0.14%
89 - 0.42%
90 - 1.20%
91 - 3.04%
92 - 6.50%
93 - 11.77%
94 - 17.69%
95 - 21.06%
96 - 19.30%
97 - 12.64%
98 - 5.19%
100 - 1.01%
Odds that passenger X is in correct seat
0 - 1.01%
1 - 99.02%
2 - 98.98%
3 - 98.98%
4 - 98.96%
5 - 98.94%
6 - 98.96%
7 - 98.94%
8 - 98.92%
9 - 98.93%
10 - 98.92%
11 - 98.91%
12 - 98.88%
13 - 98.86%
14 - 98.83%
15 - 98.83%
16 - 98.84%
17 - 98.82%
18 - 98.77%
19 - 98.77%
20 - 98.76%
21 - 98.74%
22 - 98.74%
23 - 98.70%
24 - 98.72%
25 - 98.66%
26 - 98.65%
27 - 98.65%
28 - 98.62%
29 - 98.61%
30 - 98.58%
31 - 98.56%
32 - 98.56%
33 - 98.55%
34 - 98.51%
35 - 98.49%
36 - 98.47%
37 - 98.44%
38 - 98.41%
39 - 98.38%
40 - 98.36%
41 - 98.35%
42 - 98.30%
43 - 98.27%
44 - 98.24%
45 - 98.21%
46 - 98.17%
47 - 98.16%
48 - 98.11%
49 - 98.08%
50 - 98.04%
51 - 98.00%
52 - 97.96%
53 - 97.91%
54 - 97.86%
55 - 97.84%
56 - 97.79%
57 - 97.75%
58 - 97.66%
59 - 97.63%
60 - 97.56%
61 - 97.49%
62 - 97.42%
63 - 97.38%
64 - 97.27%
65 - 97.22%
66 - 97.18%
67 - 97.05%
68 - 96.95%
69 - 96.89%
70 - 96.79%
71 - 96.69%
72 - 96.55%
73 - 96.48%
74 - 96.32%
75 - 96.17%
76 - 96.01%
77 - 95.80%
78 - 95.65%
79 - 95.49%
80 - 95.25%
81 - 94.98%
82 - 94.77%
83 - 94.47%
84 - 94.11%
85 - 93.75%
86 - 93.35%
87 - 92.86%
88 - 92.28%
89 - 91.64%
90 - 90.91%
91 - 89.98%
92 - 88.88%
93 - 87.51%
94 - 85.71%
95 - 83.32%
96 - 79.98%
97 - 75.04%
98 - 66.65%
99 - 50.08%
Distribution of correct seats
1 - 0.00%
2 - 0.03%
3 - 0.27%
4 - 1.72%
5 - 7.49%
6 - 19.81%
7 - 32.47%
8 - 28.21%
10 - 10.00%
Odds that passenger X is in correct seat
0 - 10.00%
1 - 89.96%
2 - 89.03%
3 - 87.37%
4 - 85.76%
5 - 83.30%
6 - 80.05%
7 - 74.81%
8 - 66.67%
9 - 50.11%
183 - 0.00%
184 - 0.01%
185 - 0.01%
186 - 0.05%
187 - 0.16%
188 - 0.48%
189 - 1.26%
190 - 2.84%
191 - 5.80%
192 - 10.24%
193 - 15.11%
194 - 18.58%
195 - 18.90%
196 - 14.86%
197 - 8.22%
198 - 3.00%
200 - 0.48%
Odds that passenger X is in correct seat
0 - 0.48%
1 - 99.50%
2 - 99.47%
3 - 99.49%
4 - 99.50%
5 - 99.50%
6 - 99.46%
7 - 99.47%
8 - 99.48%
9 - 99.48%
10 - 99.45%
11 - 99.49%
12 - 99.48%
13 - 99.47%
14 - 99.47%
15 - 99.47%
16 - 99.44%
17 - 99.46%
18 - 99.48%
19 - 99.48%
20 - 99.42%
21 - 99.47%
22 - 99.44%
23 - 99.44%
24 - 99.43%
25 - 99.45%
26 - 99.45%
27 - 99.40%
28 - 99.42%
29 - 99.48%
30 - 99.37%
31 - 99.39%
32 - 99.41%
33 - 99.43%
34 - 99.39%
35 - 99.37%
36 - 99.37%
37 - 99.39%
38 - 99.41%
39 - 99.36%
40 - 99.35%
41 - 99.35%
42 - 99.34%
43 - 99.37%
44 - 99.31%
45 - 99.39%
46 - 99.38%
47 - 99.34%
48 - 99.35%
49 - 99.39%
50 - 99.33%
51 - 99.32%
52 - 99.36%
53 - 99.32%
54 - 99.31%
55 - 99.26%
56 - 99.30%
57 - 99.31%
58 - 99.29%
59 - 99.28%
60 - 99.26%
61 - 99.29%
62 - 99.24%
63 - 99.23%
64 - 99.27%
65 - 99.27%
66 - 99.27%
67 - 99.23%
68 - 99.29%
69 - 99.27%
70 - 99.23%
71 - 99.23%
72 - 99.18%
73 - 99.24%
74 - 99.22%
75 - 99.18%
76 - 99.18%
77 - 99.17%
78 - 99.21%
79 - 99.20%
80 - 99.17%
81 - 99.16%
82 - 99.19%
83 - 99.17%
84 - 99.22%
85 - 99.11%
86 - 99.13%
87 - 99.17%
88 - 99.08%
89 - 99.11%
90 - 99.11%
91 - 99.14%
92 - 99.05%
93 - 99.04%
94 - 99.11%
95 - 99.02%
96 - 99.10%
97 - 99.06%
98 - 99.10%
99 - 99.02%
100 - 99.02%
101 - 99.01%
102 - 98.98%
103 - 98.99%
104 - 98.94%
105 - 98.97%
106 - 98.96%
107 - 99.01%
108 - 98.88%
109 - 98.96%
110 - 98.88%
111 - 98.90%
112 - 98.85%
113 - 98.89%
114 - 98.87%
115 - 98.82%
116 - 98.81%
117 - 98.76%
118 - 98.79%
119 - 98.82%
120 - 98.73%
121 - 98.69%
122 - 98.68%
123 - 98.72%
124 - 98.67%
125 - 98.74%
126 - 98.64%
127 - 98.69%
128 - 98.62%
129 - 98.59%
130 - 98.57%
131 - 98.52%
132 - 98.55%
133 - 98.53%
134 - 98.53%
135 - 98.48%
136 - 98.50%
137 - 98.42%
138 - 98.46%
139 - 98.41%
140 - 98.29%
141 - 98.33%
142 - 98.27%
143 - 98.33%
144 - 98.26%
145 - 98.23%
146 - 98.20%
147 - 98.26%
148 - 98.11%
149 - 98.07%
150 - 97.98%
151 - 98.05%
152 - 97.96%
153 - 97.97%
154 - 97.82%
155 - 97.86%
156 - 97.70%
157 - 97.69%
158 - 97.56%
159 - 97.64%
160 - 97.57%
161 - 97.52%
162 - 97.40%
163 - 97.30%
164 - 97.31%
165 - 97.21%
166 - 97.18%
167 - 97.12%
168 - 97.00%
169 - 96.88%
170 - 96.75%
171 - 96.78%
172 - 96.55%
173 - 96.48%
174 - 96.30%
175 - 96.14%
176 - 96.00%
177 - 95.88%
178 - 95.59%
179 - 95.50%
180 - 95.12%
181 - 94.98%
182 - 94.75%
183 - 94.34%
184 - 94.03%
185 - 93.88%
186 - 93.39%
187 - 92.81%
188 - 92.35%
189 - 91.72%
190 - 90.94%
191 - 89.97%
192 - 88.87%
193 - 87.45%
194 - 85.63%
195 - 83.42%
196 - 80.10%
197 - 75.14%
198 - 66.64%
199 - 50.12%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.