mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Merge pull request #2305 from ChJR/master
Update random.py with CPython 3.7 version.
This commit is contained in:
1 file changed
+10
-4
Vendored
+10
-4
@@ -43,11 +43,13 @@ 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
|
||||
import os as _os
|
||||
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")
|
||||
_os = None
|
||||
from _collections_abc import Set as _Set, Sequence as _Sequence
|
||||
from hashlib import sha512 as _sha512
|
||||
import itertools as _itertools
|
||||
@@ -398,7 +400,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 +552,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 +713,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 +776,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()
|
||||
Reference in new issue
Block a user