2016-01-24 13:35:54 -05:00
|
|
|
try:
|
|
|
|
import urandom as random
|
|
|
|
except ImportError:
|
2017-02-14 17:56:22 -05:00
|
|
|
try:
|
|
|
|
import random
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2016-01-24 13:35:54 -05:00
|
|
|
|
|
|
|
# check getrandbits returns a value within the bit range
|
|
|
|
for b in (1, 2, 3, 4, 16, 32):
|
|
|
|
for i in range(50):
|
|
|
|
assert random.getrandbits(b) < (1 << b)
|
|
|
|
|
|
|
|
# check that seed(0) gives a non-zero value
|
|
|
|
random.seed(0)
|
|
|
|
print(random.getrandbits(16) != 0)
|
|
|
|
|
|
|
|
# check that PRNG is repeatable
|
|
|
|
random.seed(1)
|
|
|
|
r = random.getrandbits(16)
|
|
|
|
random.seed(1)
|
|
|
|
print(random.getrandbits(16) == r)
|
2016-09-15 22:49:15 -04:00
|
|
|
|
2021-05-30 02:52:08 -04:00
|
|
|
# check that zero bits works
|
|
|
|
print(random.getrandbits(0))
|
|
|
|
|
|
|
|
# check that it throws an error for negative bits
|
2016-09-15 22:49:15 -04:00
|
|
|
try:
|
2021-05-30 02:52:08 -04:00
|
|
|
random.getrandbits(-1)
|
2016-09-15 22:49:15 -04:00
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|