2019-07-13 18:17:45 +02:00
|
|
|
import rand
|
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
const (
|
|
|
|
rnd_count = 20
|
|
|
|
seeds = [42, 256]
|
|
|
|
)
|
|
|
|
|
|
|
|
fn test_rand_reproducibility() {
|
|
|
|
for seed in seeds {
|
|
|
|
mut randoms1 := gen_randoms(seed)
|
|
|
|
mut randoms2 := gen_randoms(seed)
|
|
|
|
|
|
|
|
assert_randoms_equal(randoms1, randoms2)
|
2019-07-13 18:17:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
fn test_rand_r_reproducibility() {
|
|
|
|
for seed in seeds {
|
|
|
|
mut randoms1 := gen_randoms_r(seed)
|
|
|
|
mut randoms2 := gen_randoms_r(seed)
|
2019-07-13 18:17:45 +02:00
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
assert_randoms_equal(randoms1, randoms2)
|
2019-07-13 18:17:45 +02:00
|
|
|
}
|
2020-04-03 08:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rand_r_seed_update() {
|
|
|
|
seed := 10
|
2019-07-13 18:17:45 +02:00
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
for _ in 0..rnd_count {
|
|
|
|
prev_seed := seed
|
2020-04-27 22:53:26 +02:00
|
|
|
_ := rand.rand_r(&seed)
|
2019-07-13 18:17:45 +02:00
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
assert prev_seed != seed
|
2019-07-13 18:17:45 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 21:21:21 +02:00
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
fn gen_randoms(seed int) []int {
|
|
|
|
mut randoms := [0].repeat(rnd_count)
|
|
|
|
rand.seed(seed)
|
|
|
|
for i in 0..rnd_count {
|
|
|
|
randoms[i] = rand.next(100)
|
2019-09-16 21:21:21 +02:00
|
|
|
}
|
|
|
|
return randoms
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
fn gen_randoms_r(seed int) []int {
|
|
|
|
mut randoms := [0].repeat(rnd_count)
|
|
|
|
for i in 0..rnd_count {
|
|
|
|
randoms[i] = rand.rand_r(&seed)
|
2019-09-16 21:21:21 +02:00
|
|
|
}
|
2020-04-03 08:53:57 +02:00
|
|
|
return randoms
|
|
|
|
}
|
2019-09-16 21:21:21 +02:00
|
|
|
|
2020-04-03 08:53:57 +02:00
|
|
|
fn assert_randoms_equal(r1, r2 []int) {
|
|
|
|
for i in 0..rnd_count {
|
|
|
|
assert r1[i] == r2[i]
|
2019-09-16 21:21:21 +02:00
|
|
|
}
|
|
|
|
}
|