Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42406e6

Browse files
committed
SF patch #1191489: Simplify logic in random.py
1 parent 53e9a8b commit 42406e6

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

Lib/random.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def normalvariate(self, mu, sigma):
345345
# Math Software, 3, (1977), pp257-260.
346346

347347
random = self.random
348-
while True:
348+
while 1:
349349
u1 = random()
350350
u2 = 1.0 - random()
351351
z = NV_MAGICCONST*(u1-0.5)/u2
@@ -415,7 +415,7 @@ def vonmisesvariate(self, mu, kappa):
415415
b = (a - _sqrt(2.0 * a))/(2.0 * kappa)
416416
r = (1.0 + b * b)/(2.0 * b)
417417

418-
while True:
418+
while 1:
419419
u1 = random()
420420

421421
z = _cos(_pi * u1)
@@ -424,7 +424,7 @@ def vonmisesvariate(self, mu, kappa):
424424

425425
u2 = random()
426426

427-
if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)):
427+
if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c):
428428
break
429429

430430
u3 = random()
@@ -462,7 +462,7 @@ def gammavariate(self, alpha, beta):
462462
bbb = alpha - LOG4
463463
ccc = alpha + ainv
464464

465-
while True:
465+
while 1:
466466
u1 = random()
467467
if not 1e-7 < u1 < .9999999:
468468
continue
@@ -485,18 +485,19 @@ def gammavariate(self, alpha, beta):
485485

486486
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
487487

488-
while True:
488+
while 1:
489489
u = random()
490490
b = (_e + alpha)/_e
491491
p = b*u
492492
if p <= 1.0:
493-
x = pow(p, 1.0/alpha)
493+
x = p ** (1.0/alpha)
494494
else:
495-
# p > 1
496495
x = -_log((b-p)/alpha)
497496
u1 = random()
498-
if not (((p <= 1.0) and (u1 > _exp(-x))) or
499-
((p > 1) and (u1 > pow(x, alpha - 1.0)))):
497+
if p > 1.0:
498+
if u1 <= x ** (alpha - 1.0):
499+
break
500+
elif u1 <= _exp(-x):
500501
break
501502
return x * beta
502503

0 commit comments

Comments
 (0)