Update random.py with CPython 3.7 version.
@@ -41,17 +41,12 @@ from warnings import warn as _warn
from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
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
-try:
from os import urandom as _urandom
-except ImportError:
- # On wasm, _random.Random.random() does give a proper random value, but
- # we don't have the os module
- def _urandom(*args, **kwargs):
- raise NotImplementedError("urandom")
from _collections_abc import Set as _Set, Sequence as _Sequence
from hashlib import sha512 as _sha512
import itertools as _itertools
import bisect as _bisect
+import os as _os
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
"randrange","shuffle","normalvariate","lognormvariate",
@@ -398,7 +393,7 @@ class Random(_random.Random):
u = 1.0 - u
c = 1.0 - c
low, high = high, low
- return low + (high - low) * (u * c) ** 0.5
+ return low + (high - low) * _sqrt(u * c)
## -------------------- normal distribution --------------------
@@ -550,7 +545,7 @@ class Random(_random.Random):
return x * beta
elif alpha == 1.0:
- # expovariate(1)
+ # expovariate(1/beta)
u = random()
while u <= 1e-7:
u = random()
@@ -711,14 +706,14 @@ def _test_generator(n, func, args):
sqsum = 0.0
smallest = 1e10
largest = -1e10
- t0 = time.time()
+ t0 = time.perf_counter()
for i in range(n):
x = func(*args)
total += x
sqsum = sqsum + x*x
smallest = min(x, smallest)
largest = max(x, largest)
- t1 = time.time()
+ t1 = time.perf_counter()
print(round(t1-t0, 3), 'sec,', end=' ')
avg = total/n
stddev = _sqrt(sqsum/n - avg*avg)
@@ -774,5 +769,9 @@ getstate = _inst.getstate
setstate = _inst.setstate
getrandbits = _inst.getrandbits
+if hasattr(_os, "fork"):
+ _os.register_at_fork(after_in_child=_inst.seed)
+
+
if __name__ == '__main__':
_test()
