Skip to content

Commit 2fd9eb9

Browse files
committed
Add random.py from CPython 3.6
1 parent a20b6bf commit 2fd9eb9

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Lib/random.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@
4141
from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
4242
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
4343
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
44-
# from os import urandom as _urandom
44+
from os import urandom as _urandom
4545
from _collections_abc import Set as _Set, Sequence as _Sequence
4646
from hashlib import sha512 as _sha512
4747
import itertools as _itertools
4848
import bisect as _bisect
49-
import os as _os
5049

5150
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
5251
"randrange","shuffle","normalvariate","lognormvariate",
@@ -225,12 +224,11 @@ def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,
225224
Method=_MethodType, BuiltinMethod=_BuiltinMethodType):
226225
"Return a random int in the range [0,n). Raises ValueError if n==0."
227226

228-
# random = self.random
227+
random = self.random
229228
getrandbits = self.getrandbits
230229
# Only call self.getrandbits if the original random() builtin method
231230
# has not been overridden or if a new getrandbits() was supplied.
232-
# if type(random) is BuiltinMethod or type(getrandbits) is Method:
233-
if True:
231+
if type(random) is BuiltinMethod or type(getrandbits) is Method:
234232
k = n.bit_length() # don't use (n-1) here because n can be 1
235233
r = getrandbits(k) # 0 <= r < 2**k
236234
while r >= n:
@@ -394,7 +392,7 @@ def triangular(self, low=0.0, high=1.0, mode=None):
394392
u = 1.0 - u
395393
c = 1.0 - c
396394
low, high = high, low
397-
return low + (high - low) * _sqrt(u * c)
395+
return low + (high - low) * (u * c) ** 0.5
398396

399397
## -------------------- normal distribution --------------------
400398

@@ -546,7 +544,7 @@ def gammavariate(self, alpha, beta):
546544
return x * beta
547545

548546
elif alpha == 1.0:
549-
# expovariate(1/beta)
547+
# expovariate(1)
550548
u = random()
551549
while u <= 1e-7:
552550
u = random()
@@ -707,14 +705,14 @@ def _test_generator(n, func, args):
707705
sqsum = 0.0
708706
smallest = 1e10
709707
largest = -1e10
710-
t0 = time.perf_counter()
708+
t0 = time.time()
711709
for i in range(n):
712710
x = func(*args)
713711
total += x
714712
sqsum = sqsum + x*x
715713
smallest = min(x, smallest)
716714
largest = max(x, largest)
717-
t1 = time.perf_counter()
715+
t1 = time.time()
718716
print(round(t1-t0, 3), 'sec,', end=' ')
719717
avg = total/n
720718
stddev = _sqrt(sqsum/n - avg*avg)
@@ -748,7 +746,7 @@ def _test(N=2000):
748746

749747
_inst = Random()
750748
seed = _inst.seed
751-
# random = _inst.random
749+
random = _inst.random
752750
uniform = _inst.uniform
753751
triangular = _inst.triangular
754752
randint = _inst.randint
@@ -770,9 +768,5 @@ def _test(N=2000):
770768
setstate = _inst.setstate
771769
getrandbits = _inst.getrandbits
772770

773-
if hasattr(_os, "fork"):
774-
_os.register_at_fork(after_in_child=_inst.seed)
775-
776-
777771
if __name__ == '__main__':
778772
_test()

0 commit comments

Comments
 (0)