Created
November 23, 2018 04:49
-
-
Save bensoer/55de2391653f138f9d7f4abdeef099f9 to your computer and use it in GitHub Desktop.
Solving the prison simulation problem engineer style. 99.9% is passing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def algorithm(): | |
not_all_in_room = True | |
counter = 0 | |
prisoners = [] | |
for i in range(100): | |
prisoners.append(0) | |
while not_all_in_room: | |
counter += 1 | |
prisoner_index = random.randint(0,99) | |
prisoners[prisoner_index] += 1 | |
not_all_in_room = False | |
for prisoner in prisoners: | |
if prisoner == 0: | |
not_all_in_room = True | |
return counter | |
if __name__ == '__main__': | |
largest_count = None | |
counts = [] | |
for i in range(10000): | |
count = algorithm() | |
if largest_count is None: | |
largest_count = count | |
elif count > largest_count: | |
largest_count = count | |
counts.append(count) | |
print(largest_count) | |
#print(counts) | |
# Choose how many bins you want here | |
num_bins = 100 | |
# Use the histogram function to bin the data | |
#counts, bin_edges = np.histogram(counts, bins=num_bins, normed=True) | |
# Now find the cdf | |
#cdf = np.cumsum(counts) | |
values, bins, _ = plt.hist(counts, bins=num_bins) | |
#area = sum(np.diff(bins)*values) | |
#print(bins) | |
#sub_area = sum(np.diff(bins[0:90])*values) | |
#percentage = sub_area / area | |
#print(bins[90]) | |
#print(area) | |
#print(sub_area) | |
#print(percentage) | |
# And finally plot the cdf | |
# plt.plot(bin_edges[1:], cdf) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment