about summary refs log tree commit diff
path: root/scratch/facebook/hard/random-choice.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-12-07T20·10+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-12-07T20·10+0000
commit6c3792e881ef9295300530e61e9f21b1a1a860b5 (patch)
tree355c218e0479b5ead1484f5b6c6fac4322942dd7 /scratch/facebook/hard/random-choice.py
parentc6f6d5f33b1ef240b7d8d52e849ff00095e29a0c (diff)
Define another function to illustrate Reservoir Sampling
Documenting a few ideas about Reservoir Sampling while it's fresh on my mind.
Diffstat (limited to 'scratch/facebook/hard/random-choice.py')
-rw-r--r--scratch/facebook/hard/random-choice.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/scratch/facebook/hard/random-choice.py b/scratch/facebook/hard/random-choice.py
index 95e834a2eb01..a5c6e4e6ee81 100644
--- a/scratch/facebook/hard/random-choice.py
+++ b/scratch/facebook/hard/random-choice.py
@@ -26,6 +26,16 @@ def choose_b(m, xs):
     random.shuffle(ys)
     return ys[:m]
 
+def choose_c(m, xs):
+    """
+    This is one, possibly inefficient, way to randomly sample `m` elements from
+    `xs`.
+    """
+    choices = set()
+    while len(choices) < m:
+        choices.add(random.randint(0, len(xs) - 1))
+    return [xs[i] for i in choices]
+
 # ROYGBIV
 xs = [
     'red',
@@ -37,3 +47,4 @@ xs = [
     'violet',
 ]
 print(choose_b(3, xs))
+print(choose_c(3, xs))