Last active
June 10, 2016 18:26
-
-
Save kmod/20b49e3c655c4c74a632f7af0baf1725 to your computer and use it in GitHub Desktop.
This file contains 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
import random | |
# rand = lambda: random.normalvariate(0, 1) | |
rand = lambda: random.lognormvariate(0, 1) | |
# rand = lambda: random.expovariate(1) | |
def rand(): | |
t = 0.0 | |
N=1000 | |
P=0.1 | |
for i in xrange(N): | |
if random.random() < P: | |
t += 1.0/(N*P) | |
return t | |
# print dir(random) | |
def gen(): | |
l = [rand() for i in xrange(3)] | |
return (min(l), sum(l) / len(l)) | |
mins = [] | |
avgs = [] | |
for i in xrange(10000): | |
m, a = gen() | |
mins.append(m) | |
avgs.append(a) | |
def stddev(l): | |
s = 0 | |
ssq = 0 | |
for x in l: | |
s += x | |
ssq += x * x | |
s /= len(l) | |
ssq /= len(l) | |
return (ssq - s * s) ** 0.5 | |
print "min:", stddev(mins) | |
print "avg:", stddev(avgs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment