Create a gist now

Instantly share code, notes, and snippets.

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