In [18]:
from random import randrange, sample, shuffle
def anti_monty_hall(switch):
# Upturn the buckets
bucket = [False] * 5
# Secretly hide the treat
bucket[randrange(5)] = True
# Choose three buckets, by index
yours = sample(range(5), 3)
#
others = list(set(range(5)) - set(yours))
shuffle(others)
# Keep any index if it is to the treat, otherwise the random first
your_indx = [indx for indx in yours if bucket[indx]] or yours[:1]
other_indx = [indx for indx in others if bucket[indx]] or others[:1]
your_bucket, other_bucket = [bucket[i[0]] for i in (your_indx, other_indx)]
# Your answer to Monty hall
return other_bucket if switch else your_bucket
In [21]:
anti_monty_hall(switch=True)
Out[21]:
In [25]:
iterations = 100000
print(f"Anti Monty Hall problem simulation for {iterations} iterations in each case:")
print(f" Not switching allows you to win {sum(anti_monty_hall(switch=False) for _ in range(iterations))} times out of {iterations}")
print(f" Switching allows you to win {sum(anti_monty_hall(switch=True) for _ in range(iterations))} times out of {iterations}")
No comments:
Post a Comment