Skip to content

Instantly share code, notes, and snippets.

@gameguy43
Created November 5, 2015 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gameguy43/9138ebcf8af6f3884a31 to your computer and use it in GitHub Desktop.
Save gameguy43/9138ebcf8af6f3884a31 to your computer and use it in GitHub Desktop.
import random
def rand7():
return random.randrange(1,8)
def rand5_mod():
return rand7() % 5 + 1
def rand5_recursive():
roll = rand7()
return roll if roll <= 5 else rand5_recursive()
def rand5_while():
result = 7 # arbitrarily large
while result > 5:
result = rand7()
return result
def test_distributions():
appearances = [0] * 10
for time in range(1000000):
result = rand5_mod()
appearances[result] += 1
print appearances
appearances = [0] * 10
for time in range(1000000):
result = rand5_recursive()
appearances[result] += 1
print appearances
appearances = [0] * 10
for time in range(1000000):
result = rand5_while()
appearances[result] += 1
print appearances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment