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
|
|
|
|
|
|
|
try:
|
|
|
|
random.randint
|
|
|
|
except AttributeError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2016-01-24 13:35:54 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
print("randrange")
|
2016-01-24 13:35:54 -05:00
|
|
|
for i in range(50):
|
|
|
|
assert 0 <= random.randrange(4) < 4
|
|
|
|
assert 2 <= random.randrange(2, 6) < 6
|
|
|
|
assert -2 <= random.randrange(-2, 2) < 2
|
|
|
|
assert random.randrange(1, 9, 2) in (1, 3, 5, 7)
|
2016-09-15 22:49:15 -04:00
|
|
|
assert random.randrange(2, 1, -1) in (1, 2)
|
|
|
|
|
|
|
|
# empty range
|
|
|
|
try:
|
|
|
|
random.randrange(0)
|
|
|
|
except ValueError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("ValueError")
|
2016-09-15 22:49:15 -04:00
|
|
|
|
|
|
|
# empty range
|
|
|
|
try:
|
|
|
|
random.randrange(2, 1)
|
|
|
|
except ValueError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("ValueError")
|
2016-09-15 22:49:15 -04:00
|
|
|
|
|
|
|
# zero step
|
|
|
|
try:
|
|
|
|
random.randrange(2, 1, 0)
|
|
|
|
except ValueError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("ValueError")
|
2016-09-15 22:49:15 -04:00
|
|
|
|
|
|
|
# empty range
|
|
|
|
try:
|
|
|
|
random.randrange(2, 1, 1)
|
|
|
|
except ValueError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("ValueError")
|
2016-01-24 13:35:54 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
print("randint")
|
2016-01-24 13:35:54 -05:00
|
|
|
for i in range(50):
|
|
|
|
assert 0 <= random.randint(0, 4) <= 4
|
|
|
|
assert 2 <= random.randint(2, 6) <= 6
|
|
|
|
assert -2 <= random.randint(-2, 2) <= 2
|
|
|
|
|
2016-09-15 22:49:15 -04:00
|
|
|
# empty range
|
|
|
|
try:
|
|
|
|
random.randint(2, 1)
|
|
|
|
except ValueError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("ValueError")
|
2016-09-15 22:49:15 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
print("choice")
|
2016-01-24 13:35:54 -05:00
|
|
|
lst = [1, 2, 5, 6]
|
|
|
|
for i in range(50):
|
|
|
|
assert random.choice(lst) in lst
|
|
|
|
|
2016-09-15 22:49:15 -04:00
|
|
|
# empty sequence
|
|
|
|
try:
|
|
|
|
random.choice([])
|
|
|
|
except IndexError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("IndexError")
|
2016-09-15 22:49:15 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
print("random")
|
2016-01-24 13:35:54 -05:00
|
|
|
for i in range(50):
|
|
|
|
assert 0 <= random.random() < 1
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
print("uniform")
|
2016-01-24 13:35:54 -05:00
|
|
|
for i in range(50):
|
|
|
|
assert 0 <= random.uniform(0, 4) <= 4
|
|
|
|
assert 2 <= random.uniform(2, 6) <= 6
|
|
|
|
assert -2 <= random.uniform(-2, 2) <= 2
|