2019-07-13 19:17:45 +03:00
|
|
|
import rand
|
|
|
|
|
2020-04-03 09:53:57 +03:00
|
|
|
const (
|
2020-05-28 02:16:17 +03:00
|
|
|
rnd_count = 40
|
|
|
|
seeds = [42, 256]
|
2020-04-03 09:53:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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 19:17:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 09:53:57 +03:00
|
|
|
fn test_rand_r_reproducibility() {
|
|
|
|
for seed in seeds {
|
|
|
|
mut randoms1 := gen_randoms_r(seed)
|
|
|
|
mut randoms2 := gen_randoms_r(seed)
|
|
|
|
assert_randoms_equal(randoms1, randoms2)
|
2019-07-13 19:17:45 +03:00
|
|
|
}
|
2020-04-03 09:53:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rand_r_seed_update() {
|
|
|
|
seed := 10
|
2020-05-28 02:16:17 +03:00
|
|
|
for _ in 0 .. rnd_count {
|
2020-04-03 09:53:57 +03:00
|
|
|
prev_seed := seed
|
2020-04-27 23:53:26 +03:00
|
|
|
_ := rand.rand_r(&seed)
|
2020-04-03 09:53:57 +03:00
|
|
|
assert prev_seed != seed
|
2019-07-13 19:17:45 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 22:21:21 +03:00
|
|
|
|
2020-04-03 09:53:57 +03:00
|
|
|
fn gen_randoms(seed int) []int {
|
|
|
|
mut randoms := [0].repeat(rnd_count)
|
|
|
|
rand.seed(seed)
|
2020-05-28 02:16:17 +03:00
|
|
|
for i in 0 .. rnd_count {
|
2020-04-03 09:53:57 +03:00
|
|
|
randoms[i] = rand.next(100)
|
2019-09-16 22:21:21 +03:00
|
|
|
}
|
|
|
|
return randoms
|
|
|
|
}
|
|
|
|
|
2020-04-03 09:53:57 +03:00
|
|
|
fn gen_randoms_r(seed int) []int {
|
|
|
|
mut randoms := [0].repeat(rnd_count)
|
2020-05-28 02:16:17 +03:00
|
|
|
for i in 0 .. rnd_count {
|
2020-04-03 09:53:57 +03:00
|
|
|
randoms[i] = rand.rand_r(&seed)
|
2019-09-16 22:21:21 +03:00
|
|
|
}
|
2020-04-03 09:53:57 +03:00
|
|
|
return randoms
|
|
|
|
}
|
2019-09-16 22:21:21 +03:00
|
|
|
|
2020-04-03 09:53:57 +03:00
|
|
|
fn assert_randoms_equal(r1, r2 []int) {
|
2020-05-28 02:16:17 +03:00
|
|
|
for i in 0 .. rnd_count {
|
2020-04-03 09:53:57 +03:00
|
|
|
assert r1[i] == r2[i]
|
2019-09-16 22:21:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
|
|
|
|
fn test_rand_f32() {
|
2020-05-28 02:16:17 +03:00
|
|
|
for seed in seeds {
|
|
|
|
rand.seed(seed)
|
|
|
|
for _ in 0 .. rnd_count {
|
|
|
|
res := rand.rand_f32(100.0)
|
|
|
|
assert res >= 0.0
|
|
|
|
assert res < 100.0
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rand_f32_in_range() {
|
2020-05-28 02:16:17 +03:00
|
|
|
for seed in seeds {
|
|
|
|
rand.seed(seed)
|
|
|
|
for _ in 0 .. rnd_count {
|
|
|
|
res := rand.rand_f32_in_range(1.0, 2048.0)
|
|
|
|
assert res >= 1.0
|
|
|
|
assert res < 2048.0
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rand_f64() {
|
2020-05-28 02:16:17 +03:00
|
|
|
for seed in seeds {
|
|
|
|
rand.seed(seed)
|
|
|
|
for _ in 0 .. rnd_count {
|
|
|
|
res := rand.rand_f64(100.0)
|
|
|
|
assert res >= 0.0
|
|
|
|
assert res < 100.0
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rand_f64_in_range() {
|
2020-05-28 02:16:17 +03:00
|
|
|
for seed in seeds {
|
|
|
|
rand.seed(seed)
|
|
|
|
for _ in 0 .. rnd_count {
|
|
|
|
res := rand.rand_f64_in_range(1.0, 2048.0)
|
|
|
|
assert res >= 1.0
|
|
|
|
assert res < 2048.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
|
2020-05-28 02:16:17 +03:00
|
|
|
fn test_rand_uniform_f32() {
|
|
|
|
for seed in seeds {
|
|
|
|
rand.seed(seed)
|
|
|
|
for _ in 0 .. rnd_count {
|
|
|
|
res := rand.rand_uniform_f32()
|
|
|
|
assert res >= 0.0
|
|
|
|
assert res < 1.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
|
2020-05-28 02:16:17 +03:00
|
|
|
fn test_rand_uniform_f64() {
|
|
|
|
for seed in seeds {
|
|
|
|
rand.seed(seed)
|
|
|
|
for _ in 0 .. rnd_count {
|
|
|
|
res := rand.rand_uniform_f64()
|
|
|
|
assert res >= 0.0
|
|
|
|
assert res < 1.0
|
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
}
|