4141from types import MethodType as _MethodType , BuiltinMethodType as _BuiltinMethodType
4242from math import log as _log , exp as _exp , pi as _pi , e as _e , ceil as _ceil
4343from 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
4545from _collections_abc import Set as _Set , Sequence as _Sequence
4646from hashlib import sha512 as _sha512
4747import itertools as _itertools
4848import 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 ()
750748seed = _inst .seed
751- # random = _inst.random
749+ random = _inst .random
752750uniform = _inst .uniform
753751triangular = _inst .triangular
754752randint = _inst .randint
@@ -770,9 +768,5 @@ def _test(N=2000):
770768setstate = _inst .setstate
771769getrandbits = _inst .getrandbits
772770
773- if hasattr (_os , "fork" ):
774- _os .register_at_fork (after_in_child = _inst .seed )
775-
776-
777771if __name__ == '__main__' :
778772 _test ()
0 commit comments