Created
March 29, 2017 23:03
-
-
Save benmcmorran/abec176ce2e83b0af10ffb4a593f710b to your computer and use it in GitHub Desktop.
Prisoner Box Problem
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 | |
PRISONERS = 100 | |
def search(i, boxes): | |
next_box = i | |
for _ in range(PRISONERS // 2): | |
if i == boxes[next_box]: | |
return True | |
next_box = boxes[next_box] | |
return False | |
def run(): | |
boxes = list(range(PRISONERS)) | |
random.shuffle(boxes) | |
for i in range(PRISONERS): | |
if not search(i, boxes): | |
return False | |
return True | |
def sim(runs): | |
return sum(1 if run() else 0 for _ in range(runs)) / runs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment