Skip to content

Instantly share code, notes, and snippets.

@benmcmorran
Created March 29, 2017 23:03
Show Gist options
  • Save benmcmorran/abec176ce2e83b0af10ffb4a593f710b to your computer and use it in GitHub Desktop.
Save benmcmorran/abec176ce2e83b0af10ffb4a593f710b to your computer and use it in GitHub Desktop.
Prisoner Box Problem
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