rand: refactor rand.v and random_numbers_test.v
parent
04ca7efc0c
commit
e89ae7e194
|
@ -3,10 +3,6 @@
|
|||
// that can be found in the LICENSE file.
|
||||
module rand
|
||||
|
||||
const (
|
||||
max_i32 = 2147483647
|
||||
)
|
||||
|
||||
fn C.rand() int
|
||||
|
||||
pub fn seed(s int) {
|
||||
|
@ -29,22 +25,30 @@ pub fn rand_r(seed &int) int {
|
|||
|
||||
// rand_f32 return a random f32 between 0 and max
|
||||
pub fn rand_f32(max f32) f32 {
|
||||
return f32(f64(C.rand()) / f64(C.RAND_MAX) * f64(max))
|
||||
return rand_uniform_f32() * max
|
||||
}
|
||||
|
||||
// rand_f32 return a random f32 in range min and max
|
||||
pub fn rand_f32_in_range(min, max f32) f32 {
|
||||
scaled_r := f32(next(max_i32)) / max_i32
|
||||
return min + scaled_r * (max - min)
|
||||
return min + rand_uniform_f32() * (max - min)
|
||||
}
|
||||
|
||||
// rand_f64 return a random f64 between 0 and max
|
||||
pub fn rand_f64(max f32) f32 {
|
||||
return f64(C.rand()) / f64(C.RAND_MAX) * f64(max)
|
||||
// rand_f64 return a random f64 between 0 (inclusive) and max (exclusive)
|
||||
pub fn rand_f64(max f64) f64 {
|
||||
return rand_uniform_f64() * max
|
||||
}
|
||||
|
||||
// rand_f64 return a random f64 in range min and max
|
||||
// rand_f64 return a random f64 in range min (inclusive) and max (exclusive)
|
||||
pub fn rand_f64_in_range(min, max f64) f64 {
|
||||
scaled_r := f64(next(max_i32)) / max_i32
|
||||
return min + scaled_r * (max - min)
|
||||
return min + rand_uniform_f64() * (max - min)
|
||||
}
|
||||
|
||||
// rand_uniform_f32 returns a uniformly distributed f32 in the range 0 (inclusive) and 1 (exclusive)
|
||||
pub fn rand_uniform_f32() f32 {
|
||||
return f32(C.rand()) / f32(C.RAND_MAX)
|
||||
}
|
||||
|
||||
// rand_uniform_f64 returns a uniformly distributed f64 in the range 0 (inclusive) and 1 (exclusive)
|
||||
pub fn rand_uniform_f64() f64 {
|
||||
return f64(C.rand()) / f64(C.RAND_MAX)
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import rand
|
||||
|
||||
const (
|
||||
rnd_count = 20
|
||||
rnd_count = 40
|
||||
seeds = [42, 256]
|
||||
)
|
||||
|
||||
|
@ -9,7 +9,6 @@ fn test_rand_reproducibility() {
|
|||
for seed in seeds {
|
||||
mut randoms1 := gen_randoms(seed)
|
||||
mut randoms2 := gen_randoms(seed)
|
||||
|
||||
assert_randoms_equal(randoms1, randoms2)
|
||||
}
|
||||
}
|
||||
|
@ -18,18 +17,15 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rand_r_seed_update() {
|
||||
seed := 10
|
||||
|
||||
for _ in 0 .. rnd_count {
|
||||
prev_seed := seed
|
||||
_ := rand.rand_r(&seed)
|
||||
|
||||
assert prev_seed != seed
|
||||
}
|
||||
}
|
||||
|
@ -58,48 +54,67 @@ fn assert_randoms_equal(r1, r2 []int) {
|
|||
}
|
||||
|
||||
fn test_rand_f32() {
|
||||
|
||||
mut prev_res := f32(-1.0)
|
||||
for _ in 0..rnd_count+1 {
|
||||
res := rand.rand_f32(1.0)
|
||||
|
||||
for seed in seeds {
|
||||
rand.seed(seed)
|
||||
for _ in 0 .. rnd_count {
|
||||
res := rand.rand_f32(100.0)
|
||||
assert res >= 0.0
|
||||
assert res <= 1.0
|
||||
|
||||
prev_res = res
|
||||
assert res < 100.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn test_rand_f32_in_range() {
|
||||
|
||||
for _ in 0..rnd_count+1 {
|
||||
res := rand.rand_f32_in_range(1.0,2.0)
|
||||
|
||||
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 <= 2.0
|
||||
|
||||
// NOTE assert res != prev_res
|
||||
// ^- this kind of test can and WILL fail. Random numbers can be the same in subsequent runs
|
||||
assert res < 2048.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rand_f64() {
|
||||
|
||||
for _ in 0..rnd_count+1 {
|
||||
res := rand.rand_f64(1.0)
|
||||
|
||||
for seed in seeds {
|
||||
rand.seed(seed)
|
||||
for _ in 0 .. rnd_count {
|
||||
res := rand.rand_f64(100.0)
|
||||
assert res >= 0.0
|
||||
assert res <= 1.0
|
||||
assert res < 100.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rand_f64_in_range() {
|
||||
|
||||
for seed in seeds {
|
||||
rand.seed(seed)
|
||||
for _ in 0 .. rnd_count {
|
||||
res := rand.rand_f64_in_range(1.0,2.0)
|
||||
|
||||
res := rand.rand_f64_in_range(1.0, 2048.0)
|
||||
assert res >= 1.0
|
||||
assert res <= 2.0
|
||||
assert res < 2048.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue