2021-09-28 02:02:44 +08:00
|
|
|
import random as R
|
|
|
|
import time
|
|
|
|
import sys
|
2022-09-16 03:40:00 +08:00
|
|
|
from copy import copy
|
2021-09-28 02:02:44 +08:00
|
|
|
|
|
|
|
seed = int(time.time())
|
|
|
|
# sys.stderr.write('seed: ' + str(seed) + '\n')
|
|
|
|
R.seed(seed)
|
|
|
|
|
2022-01-24 18:32:33 +08:00
|
|
|
|
2021-09-28 02:02:44 +08:00
|
|
|
@test
|
|
|
|
def test_rnd_result(name, results, invariant):
|
|
|
|
print name
|
|
|
|
for a in results:
|
|
|
|
assert invariant(a)
|
|
|
|
|
|
|
|
|
2022-01-24 18:32:33 +08:00
|
|
|
test_rnd_result(
|
|
|
|
"randrange", [R.randrange(10) for _ in range(100)], range(10).__contains__
|
|
|
|
)
|
|
|
|
test_rnd_result(
|
|
|
|
"randrange", [R.randrange(5, 20) for _ in range(100)], range(5, 20).__contains__
|
|
|
|
)
|
|
|
|
test_rnd_result(
|
|
|
|
"randrange",
|
|
|
|
[R.randrange(9, 99, 3) for _ in range(100)],
|
|
|
|
range(9, 99, 3).__contains__,
|
|
|
|
)
|
|
|
|
test_rnd_result(
|
|
|
|
"randint", [R.randint(5, 20) for _ in range(100)], range(5, 20 + 1).__contains__
|
|
|
|
)
|
|
|
|
|
|
|
|
population = list("ABCDEFGHIJKLMNOP")
|
|
|
|
test_rnd_result(
|
|
|
|
"choice", [R.choice(population) for _ in range(100)], population.__contains__
|
|
|
|
)
|
|
|
|
test_rnd_result(
|
|
|
|
"choice", [R.choice(population) for _ in range(100)], population.__contains__
|
|
|
|
)
|
|
|
|
test_rnd_result("choices", R.choices(population), population.__contains__)
|
|
|
|
test_rnd_result("choices", R.choices(population, k=100), population.__contains__)
|
|
|
|
|
2021-09-28 02:02:44 +08:00
|
|
|
|
|
|
|
@test
|
|
|
|
def test_shuffle(v):
|
|
|
|
s = copy(v)
|
|
|
|
R.shuffle(s)
|
|
|
|
assert sorted(v) == sorted(s)
|
|
|
|
|
2022-01-24 18:32:33 +08:00
|
|
|
|
2021-09-28 02:02:44 +08:00
|
|
|
test_shuffle(list(range(100)))
|
|
|
|
|
2022-01-24 18:32:33 +08:00
|
|
|
|
2021-09-28 02:02:44 +08:00
|
|
|
@test
|
|
|
|
def test_sample(n: int, k: int):
|
|
|
|
s = R.sample(list(range(n)), k=k)
|
|
|
|
assert len(s) == k
|
|
|
|
assert len(set(s)) == len(s)
|
|
|
|
for a in s:
|
|
|
|
assert a in range(n)
|
|
|
|
|
2022-01-24 18:32:33 +08:00
|
|
|
|
2021-09-28 02:02:44 +08:00
|
|
|
test_sample(100, 5)
|
|
|
|
test_sample(100, 100)
|
|
|
|
test_sample(100, 0)
|