From 88c1b86ad13281a55378fb08e3fe015238859b16 Mon Sep 17 00:00:00 2001 From: ChJR Date: Sun, 25 Oct 2020 01:41:15 +0900 Subject: [PATCH 1/2] Update random.py with CPython 3.7 version. --- Lib/random.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 13f05f738..bb6a05f26 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -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 os import urandom as _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() From 215190e18bb5fd5013b39c8acd6757f8dacbd76a Mon Sep 17 00:00:00 2001 From: ChJR Date: Sun, 25 Oct 2020 15:44:59 +0900 Subject: [PATCH 2/2] Fix ImportError on wasm. --- Lib/random.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index bb6a05f26..c9453478a 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -41,12 +41,19 @@ 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 -from os import urandom as _urandom +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 import bisect as _bisect -import os as _os __all__ = ["Random","seed","random","uniform","randint","choice","sample", "randrange","shuffle","normalvariate","lognormvariate",