Skip to content

Instantly share code, notes, and snippets.

@antonio-catalano
Created April 8, 2018 00:38
Show Gist options
  • Save antonio-catalano/d0f770364afc3f9061f3e96463c08bc2 to your computer and use it in GitHub Desktop.
Save antonio-catalano/d0f770364afc3f9061f3e96463c08bc2 to your computer and use it in GitHub Desktop.
On average how many times do you need to roll a die before all six different numbers show up?
# On average how many times do you need to roll a die before all six different numbers show up?
import random
dado = [i for i in range (1,7)]
throw_list = []
N = 100000 # montecarlo method
for i in range(N):
lista = []
throw = 0
while len(lista) != 6 : # the while condition stops only when all the six value are in the lista
x = random.choice(dado)
if x not in lista: # we add the value in the list only if it doesn't exist yet
lista.append(x)
throw += 1
throw_list.append(throw)
print("On average i need to roll the die {} before all six numbers show up.".format(float(sum(throw_list) / N)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment