mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Minor code clean-ups (GH-20838)
This commit is contained in:
parent
9672912e8f
commit
9db5b8d448
1 changed files with 108 additions and 87 deletions
|
@ -39,7 +39,7 @@ General notes on the underlying Mersenne Twister core generator:
|
|||
|
||||
from warnings import warn as _warn
|
||||
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
|
||||
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
|
||||
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin, tau as TWOPI
|
||||
from os import urandom as _urandom
|
||||
from _collections_abc import Set as _Set, Sequence as _Sequence
|
||||
from itertools import accumulate as _accumulate, repeat as _repeat
|
||||
|
@ -54,15 +54,34 @@ except ImportError:
|
|||
from hashlib import sha512 as _sha512
|
||||
|
||||
|
||||
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
|
||||
"randrange","shuffle","normalvariate","lognormvariate",
|
||||
"expovariate","vonmisesvariate","gammavariate","triangular",
|
||||
"gauss","betavariate","paretovariate","weibullvariate",
|
||||
"getstate","setstate", "getrandbits", "choices",
|
||||
"SystemRandom"]
|
||||
__all__ = [
|
||||
"Random",
|
||||
"SystemRandom",
|
||||
"betavariate",
|
||||
"choice",
|
||||
"choices",
|
||||
"expovariate",
|
||||
"gammavariate",
|
||||
"gauss",
|
||||
"getrandbits",
|
||||
"getstate",
|
||||
"lognormvariate",
|
||||
"normalvariate",
|
||||
"paretovariate",
|
||||
"randint",
|
||||
"random",
|
||||
"randrange",
|
||||
"sample",
|
||||
"seed",
|
||||
"setstate",
|
||||
"shuffle",
|
||||
"triangular",
|
||||
"uniform",
|
||||
"vonmisesvariate",
|
||||
"weibullvariate",
|
||||
]
|
||||
|
||||
NV_MAGICCONST = 4 * _exp(-0.5) / _sqrt(2.0)
|
||||
TWOPI = 2.0*_pi
|
||||
LOG4 = _log(4.0)
|
||||
SG_MAGICCONST = 1.0 + _log(4.5)
|
||||
BPF = 53 # Number of bits in a float
|
||||
|
@ -75,6 +94,7 @@ RECIP_BPF = 2**-BPF
|
|||
|
||||
import _random
|
||||
|
||||
|
||||
class Random(_random.Random):
|
||||
"""Random number generator base class used by bound module functions.
|
||||
|
||||
|
@ -303,7 +323,8 @@ class Random(_random.Random):
|
|||
|
||||
def choice(self, seq):
|
||||
"""Choose a random element from a non-empty sequence."""
|
||||
return seq[self._randbelow(len(seq))] # raises IndexError if seq is empty
|
||||
# raises IndexError if seq is empty
|
||||
return seq[self._randbelow(len(seq))]
|
||||
|
||||
def shuffle(self, x, random=None):
|
||||
"""Shuffle list x in place, and return None.
|
||||
|
@ -412,9 +433,10 @@ class Random(_random.Random):
|
|||
if k > 5:
|
||||
setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets
|
||||
if n <= setsize:
|
||||
# An n-length list is smaller than a k-length set
|
||||
# An n-length list is smaller than a k-length set.
|
||||
# Invariant: non-selected at pool[0 : n-i]
|
||||
pool = list(population)
|
||||
for i in range(k): # invariant: non-selected at [0,n-i)
|
||||
for i in range(k):
|
||||
j = randbelow(n - i)
|
||||
result[i] = pool[j]
|
||||
pool[j] = pool[n - i - 1] # move non-selected item into vacancy
|
||||
|
@ -502,7 +524,7 @@ class Random(_random.Random):
|
|||
# Math Software, 3, (1977), pp257-260.
|
||||
|
||||
random = self.random
|
||||
while 1:
|
||||
while True:
|
||||
u1 = random()
|
||||
u2 = 1.0 - random()
|
||||
z = NV_MAGICCONST * (u1 - 0.5) / u2
|
||||
|
@ -571,7 +593,7 @@ class Random(_random.Random):
|
|||
s = 0.5 / kappa
|
||||
r = s + _sqrt(1.0 + s * s)
|
||||
|
||||
while 1:
|
||||
while True:
|
||||
u1 = random()
|
||||
z = _cos(_pi * u1)
|
||||
|
||||
|
@ -625,7 +647,7 @@ class Random(_random.Random):
|
|||
|
||||
while 1:
|
||||
u1 = random()
|
||||
if not 1e-7 < u1 < .9999999:
|
||||
if not 1e-7 < u1 < 0.9999999:
|
||||
continue
|
||||
u2 = 1.0 - random()
|
||||
v = _log(u1 / (1.0 - u1)) / ainv
|
||||
|
@ -639,11 +661,10 @@ class Random(_random.Random):
|
|||
# expovariate(1/beta)
|
||||
return -_log(1.0 - random()) * beta
|
||||
|
||||
else: # alpha is between 0 and 1 (exclusive)
|
||||
|
||||
else:
|
||||
# alpha is between 0 and 1 (exclusive)
|
||||
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
|
||||
|
||||
while 1:
|
||||
while True:
|
||||
u = random()
|
||||
b = (_e + alpha) / _e
|
||||
p = b * u
|
||||
|
@ -725,10 +746,9 @@ class Random(_random.Random):
|
|||
# This version due to Janne Sinkkonen, and matches all the std
|
||||
# texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
|
||||
y = self.gammavariate(alpha, 1.0)
|
||||
if y == 0:
|
||||
return 0.0
|
||||
else:
|
||||
if y:
|
||||
return y / (y + self.gammavariate(beta, 1.0))
|
||||
return 0.0
|
||||
|
||||
## -------------------- Pareto --------------------
|
||||
|
||||
|
@ -752,6 +772,7 @@ class Random(_random.Random):
|
|||
u = 1.0 - self.random()
|
||||
return alpha * (-_log(u)) ** (1.0 / beta)
|
||||
|
||||
|
||||
## --------------- Operating System Random Source ------------------
|
||||
|
||||
class SystemRandom(Random):
|
||||
|
@ -789,6 +810,7 @@ class SystemRandom(Random):
|
|||
raise NotImplementedError('System entropy source does not have state.')
|
||||
getstate = setstate = _notimplemented
|
||||
|
||||
|
||||
## -------------------- test program --------------------
|
||||
|
||||
def _test_generator(n, func, args):
|
||||
|
@ -809,8 +831,7 @@ def _test_generator(n, func, args):
|
|||
print(round(t1 - t0, 3), 'sec,', end=' ')
|
||||
avg = total / n
|
||||
stddev = _sqrt(sqsum / n - avg * avg)
|
||||
print('avg %g, stddev %g, min %g, max %g\n' % \
|
||||
(avg, stddev, smallest, largest))
|
||||
print('avg %g, stddev %g, min %g, max %g\n' % (avg, stddev, smallest, largest))
|
||||
|
||||
|
||||
def _test(N=2000):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue