Skip to content

Instantly share code, notes, and snippets.

@onurmatik
Created December 19, 2017 11:16
Show Gist options
  • Save onurmatik/8607ca7c59706a6156aea1a31e57aed8 to your computer and use it in GitHub Desktop.
Save onurmatik/8607ca7c59706a6156aea1a31e57aed8 to your computer and use it in GitHub Desktop.
from random import random
max_population = 10
iterations = 100000
for n in range(2, max_population):
probability = 1.0/n
success_count, fail_count = 0, 0
success_rounds, fail_rounds = [], []
for j in range(iterations):
round = 0
not_talked = n
while True:
round += 1
talked = 0
for i in range(not_talked):
if random() < probability:
talked += 1
if talked == 1:
# success case
not_talked -= 1
elif talked > 1:
# fail case
fail_count += 1
fail_rounds.append(round)
break
if not_talked == 0:
# everyone has talked
success_count += 1
success_rounds.append(round)
break
print (
n, # population
100.0 * success_count / iterations, # success percetage
100.0 * fail_count / iterations, # fail percetage
sum(success_rounds) * 1.0 / len(success_rounds), # average of rounds of successful games
sum(fail_rounds) * 1.0 / len(fail_rounds), # average of rounds of failed games
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment