41
41
from types import MethodType as _MethodType , BuiltinMethodType as _BuiltinMethodType
42
42
from math import log as _log , exp as _exp , pi as _pi , e as _e , ceil as _ceil
43
43
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
45
45
from _collections_abc import Set as _Set , Sequence as _Sequence
46
46
from hashlib import sha512 as _sha512
47
47
import itertools as _itertools
48
48
import bisect as _bisect
49
- import os as _os
50
49
51
50
__all__ = ["Random" ,"seed" ,"random" ,"uniform" ,"randint" ,"choice" ,"sample" ,
52
51
"randrange" ,"shuffle" ,"normalvariate" ,"lognormvariate" ,
@@ -225,12 +224,11 @@ def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,
225
224
Method = _MethodType , BuiltinMethod = _BuiltinMethodType ):
226
225
"Return a random int in the range [0,n). Raises ValueError if n==0."
227
226
228
- # random = self.random
227
+ random = self .random
229
228
getrandbits = self .getrandbits
230
229
# Only call self.getrandbits if the original random() builtin method
231
230
# 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 :
234
232
k = n .bit_length () # don't use (n-1) here because n can be 1
235
233
r = getrandbits (k ) # 0 <= r < 2**k
236
234
while r >= n :
@@ -394,7 +392,7 @@ def triangular(self, low=0.0, high=1.0, mode=None):
394
392
u = 1.0 - u
395
393
c = 1.0 - c
396
394
low , high = high , low
397
- return low + (high - low ) * _sqrt (u * c )
395
+ return low + (high - low ) * (u * c ) ** 0.5
398
396
399
397
## -------------------- normal distribution --------------------
400
398
@@ -546,7 +544,7 @@ def gammavariate(self, alpha, beta):
546
544
return x * beta
547
545
548
546
elif alpha == 1.0 :
549
- # expovariate(1/beta )
547
+ # expovariate(1)
550
548
u = random ()
551
549
while u <= 1e-7 :
552
550
u = random ()
@@ -707,14 +705,14 @@ def _test_generator(n, func, args):
707
705
sqsum = 0.0
708
706
smallest = 1e10
709
707
largest = - 1e10
710
- t0 = time .perf_counter ()
708
+ t0 = time .time ()
711
709
for i in range (n ):
712
710
x = func (* args )
713
711
total += x
714
712
sqsum = sqsum + x * x
715
713
smallest = min (x , smallest )
716
714
largest = max (x , largest )
717
- t1 = time .perf_counter ()
715
+ t1 = time .time ()
718
716
print (round (t1 - t0 , 3 ), 'sec,' , end = ' ' )
719
717
avg = total / n
720
718
stddev = _sqrt (sqsum / n - avg * avg )
@@ -748,7 +746,7 @@ def _test(N=2000):
748
746
749
747
_inst = Random ()
750
748
seed = _inst .seed
751
- # random = _inst.random
749
+ random = _inst .random
752
750
uniform = _inst .uniform
753
751
triangular = _inst .triangular
754
752
randint = _inst .randint
@@ -770,9 +768,5 @@ def _test(N=2000):
770
768
setstate = _inst .setstate
771
769
getrandbits = _inst .getrandbits
772
770
773
- if hasattr (_os , "fork" ):
774
- _os .register_at_fork (after_in_child = _inst .seed )
775
-
776
-
777
771
if __name__ == '__main__' :
778
772
_test ()
0 commit comments