summaryrefslogtreecommitdiff
path: root/modules/language/python/module/random.py
diff options
context:
space:
mode:
Diffstat (limited to 'modules/language/python/module/random.py')
-rw-r--r--modules/language/python/module/random.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/modules/language/python/module/random.py b/modules/language/python/module/random.py
index 0c38aa0..31783cc 100644
--- a/modules/language/python/module/random.py
+++ b/modules/language/python/module/random.py
@@ -38,7 +38,6 @@ General notes on the underlying Mersenne Twister core generator:
and is, therefore, threadsafe.
"""
-
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
@@ -183,6 +182,7 @@ class Random(_random.Random):
# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking.
istart = _int(start)
+
if istart != start:
raise ValueError("non-integer arg 1 for randrange()")
if stop is None:
@@ -192,9 +192,11 @@ class Random(_random.Random):
# stop argument supplied.
istop = _int(stop)
+
if istop != stop:
raise ValueError("non-integer stop for randrange()")
width = istop - istart
+
if step == 1 and width > 0:
return istart + self._randbelow(width)
if step == 1:
@@ -228,6 +230,7 @@ class Random(_random.Random):
random = self.random
getrandbits = self.getrandbits
+
# Only call self.getrandbits if the original random() builtin method
# has not been overridden or if a new getrandbits() was supplied.
if type(random) is BuiltinMethod or type(getrandbits) is Method:
@@ -236,6 +239,7 @@ class Random(_random.Random):
while r >= n:
r = getrandbits(k)
return r
+
# There's an overridden random() method but no new getrandbits() method,
# so we can only use random() from here.
if n >= maxsize:
@@ -243,11 +247,14 @@ class Random(_random.Random):
"enough bits to choose from a population range this large.\n"
"To remove the range limitation, add a getrandbits() method.")
return int(random() * n)
+
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
+
r = random()
while r >= limit:
r = random()
+
return int(r*maxsize) % n
## -------------------- sequence methods -------------------
@@ -696,8 +703,9 @@ class SystemRandom(Random):
## -------------------- test program --------------------
+import time
+
def _test_generator(n, func, args):
- import time
print(n, 'times', func.__name__)
total = 0.0
sqsum = 0.0
@@ -710,10 +718,12 @@ def _test_generator(n, func, args):
sqsum = sqsum + x*x
smallest = min(x, smallest)
largest = max(x, largest)
+
t1 = time.time()
print(round(t1-t0, 3), 'sec,', end=' ')
avg = total/n
stddev = _sqrt(sqsum/n - avg*avg)
+
print('avg %g, stddev %g, min %g, max %g\n' % \
(avg, stddev, smallest, largest))
@@ -766,5 +776,3 @@ getstate = _inst.getstate
setstate = _inst.setstate
getrandbits = _inst.getrandbits
-if __name__ == '__main__':
- _test()