Created
June 23, 2017 05:02
-
-
Save DadgadCafe/c790a3349fbe40747fe0acf2b290fe59 to your computer and use it in GitHub Desktop.
some algorithm...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://sahandsaba.com/interview-question-groupon-probability.html | |
import random | |
def bernoulli_process(p): | |
if p > 1.0 or p < 0.0: | |
raise ValueError("p should be between 0.0 and 1.0.") | |
while True: | |
yield random.random() > p | |
def von_neumann_extractor(process): | |
while True: | |
x, y = next(process), next(process) | |
if x != y: # only events: p * (1-p), (1-p) * p | |
yield x # yield y is also fine. | |
def test(n): | |
g = von_neumann_extractor(bernoulli_process(0.1)) # whatever from 0 - 1 | |
count = 0 | |
t = 0 | |
while count < n: | |
count += 1 | |
if next(g): | |
t += 1 | |
return t/count | |
test(10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment