mullers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def mullers_single(mu, sigma, single=True):
while True:
x, y = np.random.ranf(2)*2 - 1
s = x**2 + y**2
if 0 < s <= 1:
break
logs = math.sqrt(-2.*math.log(s) / s)
z0 = x * logs
z1 = y * logs
if single:
return z0 * sigma + mu
else:
return [z0 * sigma + mu, z1 * sigma + mu]
def mullers(mu, sigma, nsize=1):
if nsize == 1:
return mullers_single(mu, sigma)
res = []
for i in xrange(nsize / 2):
res += mullers_single(mu, sigma, single=False)
if nsize % 2 != 0:
res.append(mullers_single(mu, sigma))
return res