Define another function to illustrate Reservoir Sampling
Documenting a few ideas about Reservoir Sampling while it's fresh on my mind.
This commit is contained in:
parent
c6f6d5f33b
commit
6c3792e881
1 changed files with 11 additions and 0 deletions
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue