Include re-roll strategy for rand7
After seeing the solution that my book advocated, I implemented it using recursion.
This commit is contained in:
parent
5820f6f459
commit
09cd819a70
1 changed files with 11 additions and 8 deletions
|
@ -8,15 +8,18 @@ from collections import Counter
|
||||||
def rand5():
|
def rand5():
|
||||||
return random.randint(0,4)
|
return random.randint(0,4)
|
||||||
|
|
||||||
def rand4():
|
|
||||||
x = rand7()
|
|
||||||
if x >= 4:
|
|
||||||
return rand4()
|
|
||||||
return x
|
|
||||||
|
|
||||||
# Return [0,6]
|
# Return [0,6]
|
||||||
def rand7():
|
def rand7_a():
|
||||||
return sum(rand5() for _ in range(7)) % 7
|
return sum(rand5() for _ in range(7)) % 7
|
||||||
|
|
||||||
c = Counter([rand4() for _ in range(100000)])
|
# Return [0,6]
|
||||||
|
def rand7_b():
|
||||||
|
x = 5 * rand5() + rand5()
|
||||||
|
if x < 21:
|
||||||
|
return x % 7
|
||||||
|
return rand7_b()
|
||||||
|
|
||||||
|
c = Counter([rand7_a() for _ in range(100000)])
|
||||||
|
print(c)
|
||||||
|
c = Counter([rand7_b() for _ in range(100000)])
|
||||||
print(c)
|
print(c)
|
||||||
|
|
Loading…
Reference in a new issue