Example 11-6. Implementation of probabilistic counting algorithm
def computeK(generator):
"""
Compute estimate of using probabilistic counting algorithm.
Doesn't know value of n, the size of the population.
"""
seen = set()
while True:
item = generator()
if item in seen:
k = len(seen)
return 2.0*k*k/math.pi
else:
seen.add(item)
Let’s start with some intuition. We must have the ability to pick random elements
from the set and mark them as being seen. Since we assume the set is finite, at some
point ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.