mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
rand: add random functions for f32 and f64, [0,max] and [min,max] versions
This commit is contained in:
parent
e0db880791
commit
8c753ddf8d
2 changed files with 69 additions and 0 deletions
|
@ -56,3 +56,50 @@ fn assert_randoms_equal(r1, r2 []int) {
|
|||
assert r1[i] == r2[i]
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rand_f32() {
|
||||
|
||||
mut prev_res := f32(-1.0)
|
||||
for _ in 0..rnd_count+1 {
|
||||
res := rand.rand_f32(1.0)
|
||||
|
||||
assert res >= 0.0
|
||||
assert res <= 1.0
|
||||
|
||||
prev_res = res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn test_rand_f32_in_range() {
|
||||
|
||||
for _ in 0..rnd_count+1 {
|
||||
res := rand.rand_f32_in_range(1.0,2.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
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rand_f64() {
|
||||
|
||||
for _ in 0..rnd_count+1 {
|
||||
res := rand.rand_f64(1.0)
|
||||
|
||||
assert res >= 0.0
|
||||
assert res <= 1.0
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rand_f64_in_range() {
|
||||
|
||||
for _ in 0..rnd_count {
|
||||
res := rand.rand_f64_in_range(1.0,2.0)
|
||||
|
||||
assert res >= 1.0
|
||||
assert res <= 2.0
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue