about summary refs log tree commit diff
path: root/scratch/facebook/moderate/rand7.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-11-14T17·36+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-11-14T17·36+0000
commit09cd819a70b0b647960f0d8f55d628c78779592d (patch)
tree9facd399608265031857d59c8bd26c3097048d71 /scratch/facebook/moderate/rand7.py
parent5820f6f45903d90779d449cf4b24fbdaff17a285 (diff)
Include re-roll strategy for rand7
After seeing the solution that my book advocated, I implemented it using
recursion.
Diffstat (limited to 'scratch/facebook/moderate/rand7.py')
-rw-r--r--scratch/facebook/moderate/rand7.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/scratch/facebook/moderate/rand7.py b/scratch/facebook/moderate/rand7.py
index 19a96ed77b51..ed3a7cea80e5 100644
--- a/scratch/facebook/moderate/rand7.py
+++ b/scratch/facebook/moderate/rand7.py
@@ -8,15 +8,18 @@ from collections import Counter
 def rand5():
     return random.randint(0,4)
 
-def rand4():
-    x = rand7()
-    if x >= 4:
-        return rand4()
-    return x
-
 # Return [0,6]
-def rand7():
+def rand7_a():
     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)