Created
November 5, 2015 08:42
-
-
Save gameguy43/9138ebcf8af6f3884a31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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