-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathreservoir-sampling.py
53 lines (32 loc) · 1.1 KB
/
reservoir-sampling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
#15
Facebook
Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.
"""
import random
from datetime import datetime
class Stream:
def __init__(self):
self.pseudoStream = [random.randint(-10000, 10000)]*10000000+[None]
def nextSample(self):
return random.choice(self.pseudoStream)
def selectSample():
sampleCount = 1
stream = Stream()
selectedSample = stream.nextSample()
maxProbability = random.random()
while True:
currentSample = stream.nextSample()
if currentSample == None:
print("Received", sampleCount, "samples.")
return selectedSample
sampleCount += 1
currentProbability = random.random()
if currentProbability > maxProbability:
selectedSample = currentSample
maxProbability = currentProbability
if __name__ == "__main__":
startTime = datetime.now()
print("Chosen sample:", selectSample() )
stopTime = datetime.now()
print("Time elapsed: ", stopTime-startTime)