1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: fix warnings

This commit is contained in:
ka-weihe 2020-06-02 06:39:38 +02:00 committed by GitHub
parent 076089d3c5
commit 13c68eb81e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 90 additions and 448 deletions

View File

@ -1,44 +0,0 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.rand
fn get_random_bytes(no_bytes int) []byte {
r := rand.read(no_bytes) or {
assert false
return []
}
assert r.len == no_bytes
return r
}
fn test_crypto_rand_read() {
no_bytes := 100
r1 := get_random_bytes(no_bytes)
r2 := get_random_bytes(no_bytes)
mut equals := 0
for i in 0 .. r1.len {
if r1[i] == r2[i] {
equals++
}
}
assert (100.0 * f32(equals) / f32(no_bytes)) < 20.0
}
fn test_crypto_rand_int_u64() {
max := u64(160)
mut unique := []u64{}
for _ in 0 .. 80 {
r := rand.int_u64(max) or {
assert false
return
}
if r >= max {
assert false
}
if r !in unique {
unique << r
}
}
assert unique.len >= 10
}

View File

@ -234,7 +234,7 @@ pub fn (mut rng MT19937RNG) intn(max int) int {
eprintln('max has to be positive.')
exit(1)
}
return int(rng.u32n(max))
return int(rng.u32n(u32(max)))
}
// rng.i64n(max) - return a 64bit positive i64 in [0, max)
@ -244,7 +244,7 @@ pub fn (mut rng MT19937RNG) i64n(max i64) i64 {
eprintln('max has to be positive.')
exit(1)
}
return i64(rng.u64n(max))
return i64(rng.u64n(u64(max)))
}
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)

View File

@ -14,7 +14,7 @@ const (
)
fn mt19937_basic_test() {
rng := rand.MT19937RNG{}
mut rng := rand.MT19937RNG{}
rng.seed([u32(0xdeadbeef)])
target := [956529277, 3842322136, 3319553134, 1843186657, 2704993644, 595827513, 938518626,
1676224337, 3221315650, 1819026461]
@ -73,7 +73,7 @@ fn test_mt19937_variability() {
}
}
fn check_uniformity_u64(rng rand.MT19937RNG, range u64) {
fn check_uniformity_u64(mut rng rand.MT19937RNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
@ -94,12 +94,12 @@ fn test_mt19937_uniformity_u64() {
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for range in ranges {
check_uniformity_u64(rng, range)
check_uniformity_u64(mut rng, u64(range))
}
}
}
fn check_uniformity_f64(rng rand.MT19937RNG) {
fn check_uniformity_f64(mut rng rand.MT19937RNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
@ -118,7 +118,7 @@ fn test_mt19937_uniformity_f64() {
for seed in seeds {
mut rng := rand.MT19937RNG{}
rng.seed(seed)
check_uniformity_f64(rng)
check_uniformity_f64(mut rng)
}
}
@ -128,7 +128,7 @@ fn test_mt19937_u32n() {
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(u32(max))
assert value >= 0
assert value < max
}
@ -155,7 +155,7 @@ fn test_mt19937_u32_in_range() {
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(u32(min), u32(max))
assert value >= min
assert value < max
}
@ -210,7 +210,7 @@ fn test_mt19937_int63() {
fn test_mt19937_intn() {
max := 2525642
for seed in seeds {
rng := rand.MT19937RNG{}
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
@ -223,7 +223,7 @@ fn test_mt19937_intn() {
fn test_mt19937_i64n() {
max := i64(3246727724653636)
for seed in seeds {
rng := rand.MT19937RNG{}
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
@ -237,7 +237,7 @@ fn test_mt19937_int_in_range() {
min := -4252
max := 1034
for seed in seeds {
rng := rand.MT19937RNG{}
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
@ -251,7 +251,7 @@ fn test_mt19937_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
rng := rand.MT19937RNG{}
mut rng := rand.MT19937RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)

View File

@ -106,7 +106,7 @@ pub fn (mut rng MuslRNG) u32_in_range(min, max u64) u64 {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.u32n(max - min)
return min + rng.u32n(u32(max - min))
}
// rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
@ -150,7 +150,7 @@ pub fn (mut rng MuslRNG) intn(max int) int {
eprintln('max has to be positive.')
exit(1)
}
return int(rng.u32n(max))
return int(rng.u32n(u32(max)))
}
// rng.i64n(max) - return a 64bit positive i64 in [0, max)
@ -160,7 +160,7 @@ pub fn (mut rng MuslRNG) i64n(max i64) i64 {
eprintln('max has to be positive.')
exit(1)
}
return i64(rng.u64n(max))
return i64(rng.u64n(u64(max)))
}
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)

View File

@ -63,7 +63,7 @@ fn test_musl_variability() {
}
}
fn check_uniformity_u64(rng rand.MuslRNG, range u64) {
fn check_uniformity_u64(mut rng rand.MuslRNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
@ -84,12 +84,12 @@ fn test_musl_uniformity_u64() {
mut rng := rand.MuslRNG{}
rng.seed(seed)
for range in ranges {
check_uniformity_u64(rng, range)
check_uniformity_u64(mut rng, u64(range))
}
}
}
fn check_uniformity_f64(rng rand.MuslRNG) {
fn check_uniformity_f64(mut rng rand.MuslRNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
@ -108,7 +108,7 @@ fn test_musl_uniformity_f64() {
for seed in seeds {
mut rng := rand.MuslRNG{}
rng.seed(seed)
check_uniformity_f64(rng)
check_uniformity_f64(mut rng)
}
}
@ -118,7 +118,7 @@ fn test_musl_u32n() {
mut rng := rand.MuslRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(u32(max))
assert value >= 0
assert value < max
}
@ -145,7 +145,7 @@ fn test_musl_u32_in_range() {
mut rng := rand.MuslRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(u64(min), u64(max))
assert value >= min
assert value < max
}
@ -200,7 +200,7 @@ fn test_musl_int63() {
fn test_musl_intn() {
max := 2525642
for seed in seeds {
rng := rand.MuslRNG{}
mut rng := rand.MuslRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
@ -213,7 +213,7 @@ fn test_musl_intn() {
fn test_musl_i64n() {
max := i64(3246727724653636)
for seed in seeds {
rng := rand.MuslRNG{}
mut rng := rand.MuslRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
@ -227,7 +227,7 @@ fn test_musl_int_in_range() {
min := -4252
max := 1034
for seed in seeds {
rng := rand.MuslRNG{}
mut rng := rand.MuslRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
@ -241,7 +241,7 @@ fn test_musl_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
rng := rand.MuslRNG{}
mut rng := rand.MuslRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)

View File

@ -14,7 +14,7 @@ mut:
// TODO: Remove in Phase 2 of reorganizing Random
pub fn new_pcg32(init_state, init_seq u64) PCG32RNG {
rng := PCG32RNG{}
mut rng := PCG32RNG{}
rng.seed([u32(init_state), u32(init_state >> 32), u32(init_seq), u32(init_seq >> 32)])
return rng
}
@ -104,7 +104,7 @@ pub fn (mut rng PCG32RNG) u32_in_range(min, max u64) u64 {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.u32n(max - min)
return min + rng.u32n(u32(max - min))
}
// rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
@ -148,7 +148,7 @@ pub fn (mut rng PCG32RNG) intn(max int) int {
eprintln('max has to be positive.')
exit(1)
}
return int(rng.u32n(max))
return int(rng.u32n(u32(max)))
}
// rng.i64n(max) - return a 64bit positive i64 in [0, max)
@ -158,7 +158,7 @@ pub fn (mut rng PCG32RNG) i64n(max i64) i64 {
eprintln('max has to be positive.')
exit(1)
}
return i64(rng.u64n(max))
return i64(rng.u64n(u64(max)))
}
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)

View File

@ -61,7 +61,7 @@ fn test_pcg32_variability() {
}
}
fn check_uniformity_u64(rng rand.PCG32RNG, range u64) {
fn check_uniformity_u64(mut rng rand.PCG32RNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
@ -82,12 +82,12 @@ fn test_pcg32_uniformity_u64() {
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for range in ranges {
check_uniformity_u64(rng, range)
check_uniformity_u64(mut rng, u64(range))
}
}
}
fn check_uniformity_f64(rng rand.PCG32RNG) {
fn check_uniformity_f64(mut rng rand.PCG32RNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
@ -106,7 +106,7 @@ fn test_pcg32_uniformity_f64() {
for seed in seeds {
mut rng := rand.PCG32RNG{}
rng.seed(seed)
check_uniformity_f64(rng)
check_uniformity_f64(mut rng)
}
}
@ -116,7 +116,7 @@ fn test_pcg32_u32n() {
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(u32(max))
assert value >= 0
assert value < max
}
@ -143,7 +143,7 @@ fn test_pcg32_u32_in_range() {
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(u64(min), u64(max))
assert value >= min
assert value < max
}
@ -198,7 +198,7 @@ fn test_pcg32_int63() {
fn test_pcg32_intn() {
max := 2525642
for seed in seeds {
rng := rand.PCG32RNG{}
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
@ -211,7 +211,7 @@ fn test_pcg32_intn() {
fn test_pcg32_i64n() {
max := i64(3246727724653636)
for seed in seeds {
rng := rand.PCG32RNG{}
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
@ -225,7 +225,7 @@ fn test_pcg32_int_in_range() {
min := -4252
max := 1034
for seed in seeds {
rng := rand.PCG32RNG{}
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
@ -239,7 +239,7 @@ fn test_pcg32_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
rng := rand.PCG32RNG{}
mut rng := rand.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)

View File

@ -27,10 +27,6 @@ pub fn rand_r(seed &int) int {
return ns & 0x7fffffff
}
const (
default_rng = new_default({})
)
pub struct PRNGConfigStruct {
seed []u32 = time_seed_array(2)
}
@ -41,109 +37,6 @@ pub fn new_default(config PRNGConfigStruct) &WyRandRNG {
return rng
}
// u32() - returns a uniformly distributed pseudorandom 32-bit unsigned u32
pub fn u32() u32 {
return default_rng.u32()
}
// u64() - returns a uniformly distributed pseudorandom 64-bit unsigned u64
pub fn u64() u64 {
return default_rng.u64()
}
// u32n(max) - returns a uniformly distributed pseudorandom 32-bit unsigned u32 in [0, max)
pub fn u32n(max u32) u32 {
return default_rng.u32n(max)
}
// u64n(max) - returns a uniformly distributed pseudorandom 64-bit unsigned u64 in [0, max)
pub fn u64n(max u64) u64 {
return default_rng.u64n(max)
}
// u32n() - returns a uniformly distributed pseudorandom 32-bit unsigned u32 in [min, max)
pub fn u32_in_range(min, max u32) u32 {
return default_rng.u32_in_range(min, max)
}
// u64_in_range(min, max) - returns a uniformly distributed pseudorandom 64-bit unsigned u64 in [min, max)
pub fn u64_in_range(min, max u64) u64 {
return default_rng.u64_in_range(min, max)
}
// int() - returns a uniformly distributed pseudorandom 32-bit signed (possibly negative) int
pub fn int() int {
return default_rng.int()
}
// intn(max) - returns a uniformly distributed pseudorandom 32-bit signed positive int in [0, max)
pub fn intn(max int) int {
return default_rng.intn(max)
}
// int_in_range(min, max) - returns a uniformly distributed pseudorandom
// 32-bit signed int in [min, max)
pub fn int_in_range(min, max int) int {
return default_rng.int_in_range(min, max)
}
// int31() - returns a uniformly distributed pseudorandom 31-bit signed positive int
pub fn int31() int {
return default_rng.int31()
}
// i64() - returns a uniformly distributed pseudorandom 64-bit signed (possibly negative) i64
pub fn i64() i64 {
return default_rng.i64()
}
// i64n(max) - returns a uniformly distributed pseudorandom 64-bit signed positive i64 in [0, max)
pub fn i64n(max i64) i64 {
return default_rng.i64n(max)
}
// i64_in_range(min, max) - returns a uniformly distributed pseudorandom
// 64-bit signed int in [min, max)
pub fn i64_in_range(min, max i64) i64 {
return default_rng.i64_in_range(min, max)
}
// int63() - returns a uniformly distributed pseudorandom 63-bit signed positive int
pub fn int63() i64 {
return default_rng.int63()
}
// f32() - returns a uniformly distributed 32-bit floating point in [0, 1)
pub fn f32() f32 {
return default_rng.f32()
}
// f64() - returns a uniformly distributed 64-bit floating point in [0, 1)
pub fn f64() f64 {
return default_rng.f64()
}
// f32n() - returns a uniformly distributed 32-bit floating point in [0, max)
pub fn f32n(max f32) f32 {
return default_rng.f32n(max)
}
// f64n() - returns a uniformly distributed 64-bit floating point in [0, max)
pub fn f64n(max f64) f64 {
return default_rng.f64n(max)
}
// f32_in_range(min, max) - returns a uniformly distributed 32-bit floating point in [min, max)
pub fn f32_in_range(min, max f32) f32 {
return default_rng.f32_in_range(min, max)
}
// f64_in_range(min, max) - returns a uniformly distributed 64-bit floating point in [min, max)
pub fn f64_in_range(min, max f64) f64 {
return default_rng.f64_in_range(min, max)
}
// rand_f32 return a random f32 between 0 and max
[deprecated]
pub fn rand_f32(max f32) f32 {

View File

@ -54,210 +54,3 @@ fn assert_randoms_equal(r1, r2 []int) {
}
}
fn test_rand_f32_old() {
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
}
}
}
fn test_rand_f32_in_range_old() {
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
}
}
}
fn test_rand_f64_old() {
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
}
}
}
fn test_rand_f64_in_range_old() {
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
}
}
}
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
}
}
}
fn test_rand_u32n() {
max := u32(4287502)
for _ in 0 .. rnd_count {
assert rand.u32n(max) < max
}
}
fn test_rand_u64n() {
max := u64(23442353534587502)
for _ in 0 .. rnd_count {
assert rand.u64n(max) < max
}
}
fn test_rand_u32_in_range() {
min := u32(5256)
max := u32(4287502)
for _ in 0 .. rnd_count {
value := rand.u32_in_range(min, max)
assert value >= min
assert value < max
}
}
fn test_rand_u64_in_range() {
min := u64(4265266246)
max := u64(23442353534587502)
for _ in 0 .. rnd_count {
value := rand.u64_in_range(min, max)
assert value >= min
assert value < max
}
}
fn test_rand_intn() {
max := 720948723
for _ in 0 .. rnd_count {
value := rand.intn(max)
assert value >= 0
assert value < max
}
}
fn test_rand_i64n() {
max := i64(209487239094)
for _ in 0 .. rnd_count {
value := rand.i64n(max)
assert value >= 0
assert value < max
}
}
fn test_rand_int_in_range() {
min := -34058
max := -10542
for _ in 0 .. rnd_count {
value := rand.int_in_range(min, max)
assert value >= min
assert value < max
}
}
fn test_rand_i64_in_range() {
min := i64(-5026245)
max := i64(209487239094)
for _ in 0 .. rnd_count {
value := rand.i64_in_range(min, max)
assert value >= min
assert value < max
}
}
fn test_rand_int31() {
for _ in 0 .. rnd_count {
value := rand.int31()
assert value >= 0
assert value <= math.max_i32
}
}
fn test_rand_int63() {
for _ in 0 .. rnd_count {
value := rand.int63()
assert value >= 0
assert value <= math.max_i64
}
}
fn test_rand_f32() {
for _ in 0 .. rnd_count {
value := rand.f32()
assert value >= 0.0
assert value < 1.0
}
}
fn test_rand_f64() {
for _ in 0 .. rnd_count {
value := rand.f64()
assert value >= 0.0
assert value < 1.0
}
}
fn test_rand_f32n() {
max := f32(34.52)
for _ in 0 .. rnd_count {
value := rand.f32n(max)
assert value >= 0.0
assert value < max
}
}
fn test_rand_f64n() {
max := 3495.2
for _ in 0 .. rnd_count {
value := rand.f64n(max)
assert value >= 0.0
assert value < max
}
}
fn test_rand_f32_in_range() {
min := f32(-10.4)
max := f32(43.2)
for _ in 0 .. rnd_count {
value := rand.f32_in_range(min, max)
assert value >= min
assert value < max
}
}
fn test_rand_f64_in_range() {
min := -10980.4
max := -2.0
for _ in 0 .. rnd_count {
value := rand.f64_in_range(min, max)
assert value >= min
assert value < max
}
}

View File

@ -135,7 +135,7 @@ pub fn (mut rng SplitMix64RNG) intn(max int) int {
eprintln('max has to be positive.')
exit(1)
}
return int(rng.u32n(max))
return int(rng.u32n(u32(max)))
}
// rng.i64n(max) returns a pseudorandom int that lies in [0, max)
@ -145,7 +145,7 @@ pub fn (mut rng SplitMix64RNG) i64n(max i64) i64 {
eprintln('max has to be positive.')
exit(1)
}
return i64(rng.u64n(max))
return i64(rng.u64n(u64(max)))
}
// rng.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)

View File

@ -63,7 +63,7 @@ fn test_splitmix64_variability() {
}
}
fn check_uniformity_u64(rng rand.SplitMix64RNG, range u64) {
fn check_uniformity_u64(mut rng rand.SplitMix64RNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
@ -84,12 +84,12 @@ fn test_splitmix64_uniformity_u64() {
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for range in ranges {
check_uniformity_u64(rng, range)
check_uniformity_u64(mut rng, u64(range))
}
}
}
fn check_uniformity_f64(rng rand.SplitMix64RNG) {
fn check_uniformity_f64(mut rng rand.SplitMix64RNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
@ -108,7 +108,7 @@ fn test_splitmix64_uniformity_f64() {
for seed in seeds {
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
check_uniformity_f64(rng)
check_uniformity_f64(mut rng)
}
}
@ -118,7 +118,7 @@ fn test_splitmix64_u32n() {
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(u32(max))
assert value >= 0
assert value < max
}
@ -145,7 +145,7 @@ fn test_splitmix64_u32_in_range() {
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(u32(min), u32(max))
assert value >= min
assert value < max
}
@ -200,7 +200,7 @@ fn test_splitmix64_int63() {
fn test_splimix64_intn() {
max := 2525642
for seed in seeds {
rng := rand.SplitMix64RNG{}
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
@ -213,7 +213,7 @@ fn test_splimix64_intn() {
fn test_splimix64_i64n() {
max := i64(3246727724653636)
for seed in seeds {
rng := rand.SplitMix64RNG{}
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
@ -227,7 +227,7 @@ fn test_splimix64_int_in_range() {
min := -4252
max := 230549862
for seed in seeds {
rng := rand.SplitMix64RNG{}
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
@ -241,7 +241,7 @@ fn test_splimix64_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
rng := rand.SplitMix64RNG{}
mut rng := rand.SplitMix64RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)

View File

@ -204,7 +204,7 @@ pub fn (r SysRNG) intn(max int) int {
eprintln('max has to be positive.')
exit(1)
}
return int(r.u32n(max))
return int(r.u32n(u32(max)))
}
// r.i64n(max) returns a pseudorandom i64 that lies in [0, max)
@ -214,7 +214,7 @@ pub fn (r SysRNG) i64n(max i64) i64 {
eprintln('max has to be positive.')
exit(1)
}
return i64(r.u64n(max))
return i64(r.u64n(u64(max)))
}
// r.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)

View File

@ -28,8 +28,8 @@ fn test_sys_rng_reproducibility() {
// seed for another batch of data.
for seed in seeds {
seed_data := [seed]
r1 := rand.SysRNG{}
r2 := rand.SysRNG{}
mut r1 := rand.SysRNG{}
mut r2 := rand.SysRNG{}
r1.seed(seed_data)
ints1 := get_n_randoms(value_count, r1)
r2.seed(seed_data)
@ -55,7 +55,7 @@ fn test_sys_rng_variability() {
// improbable but not impossible.
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
mut values := []u64{cap: value_count}
for i in 0 .. value_count {
@ -88,10 +88,10 @@ fn test_sys_rng_uniformity_u64() {
ranges := [14019545, 80240, 130]
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for range in ranges {
check_uniformity_u64(rng, range)
check_uniformity_u64(rng, u64(range))
}
}
}
@ -114,7 +114,7 @@ fn test_sys_rng_uniformity_f64() {
// The f64 version
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
check_uniformity_f64(rng)
}
@ -124,10 +124,10 @@ fn test_sys_rng_u32n() {
max := 16384
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(u32(max))
assert value >= 0
assert value < max
}
@ -138,7 +138,7 @@ fn test_sys_rng_u64n() {
max := u64(379091181005)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.u64n(max)
@ -153,10 +153,10 @@ fn test_sys_rng_u32_in_range() {
min := 316846
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(u32(min), u32(max))
assert value >= min
assert value < max
}
@ -168,7 +168,7 @@ fn test_sys_rng_u64_in_range() {
min := u64(6848646868)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.u64_in_range(min, max)
@ -182,7 +182,7 @@ fn test_sys_rng_intn() {
max := 2525642
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.intn(max)
@ -196,7 +196,7 @@ fn test_sys_rng_i64n() {
max := i64(3246727724653636)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.i64n(max)
@ -211,7 +211,7 @@ fn test_sys_rng_int_in_range() {
max := 23054962
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
@ -226,7 +226,7 @@ fn test_sys_rng_i64_in_range() {
max := i64(324058)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)
@ -241,7 +241,7 @@ fn test_sys_rng_int31() {
sign_mask := 0x80000000
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.int31()
@ -258,7 +258,7 @@ fn test_sys_rng_int63() {
sign_mask := i64(0x8000000000000000)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.int63()
@ -272,7 +272,7 @@ fn test_sys_rng_int63() {
fn test_sys_rng_f32() {
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.f32()
@ -285,7 +285,7 @@ fn test_sys_rng_f32() {
fn test_sys_rng_f64() {
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.f64()
@ -299,7 +299,7 @@ fn test_sys_rng_f32n() {
max := f32(357.0)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.f32()
@ -313,7 +313,7 @@ fn test_sys_rng_f64n() {
max := 1.52e6
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.f64()
@ -328,7 +328,7 @@ fn test_sys_rng_f32_in_range() {
max := f32(125.0)
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.f32()
@ -343,7 +343,7 @@ fn test_sys_rng_f64_in_range() {
max := 5015.2
for seed in seeds {
seed_data := [seed]
rng := rand.SysRNG{}
mut rng := rand.SysRNG{}
rng.seed(seed_data)
for _ in 0 .. range_limit {
value := rng.f64()

View File

@ -164,7 +164,7 @@ pub fn (mut rng WyRandRNG) intn(max int) int {
eprintln('max has to be positive.')
exit(1)
}
return int(rng.u32n(max))
return int(rng.u32n(u32(max)))
}
// rng.i64n(max) returns a pseudorandom int that lies in [0, max)
@ -174,7 +174,7 @@ pub fn (mut rng WyRandRNG) i64n(max i64) i64 {
eprintln('max has to be positive.')
exit(1)
}
return i64(rng.u64n(max))
return i64(rng.u64n(u64(max)))
}
// rng.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)

View File

@ -63,7 +63,7 @@ fn test_wyrand_variability() {
}
}
fn check_uniformity_u64(rng rand.WyRandRNG, range u64) {
fn check_uniformity_u64(mut rng rand.WyRandRNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
@ -84,12 +84,12 @@ fn test_wyrand_uniformity_u64() {
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for range in ranges {
check_uniformity_u64(rng, range)
check_uniformity_u64(mut rng, u64(range))
}
}
}
fn check_uniformity_f64(rng rand.WyRandRNG) {
fn check_uniformity_f64(mut rng rand.WyRandRNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
@ -108,7 +108,7 @@ fn test_wyrand_uniformity_f64() {
for seed in seeds {
mut rng := rand.WyRandRNG{}
rng.seed(seed)
check_uniformity_f64(rng)
check_uniformity_f64(mut rng)
}
}
@ -118,7 +118,7 @@ fn test_wyrand_u32n() {
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(u32(max))
assert value >= 0
assert value < max
}
@ -145,7 +145,7 @@ fn test_wyrand_u32_in_range() {
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(u32(min), u32(max))
assert value >= min
assert value < max
}
@ -200,7 +200,7 @@ fn test_wyrand_int63() {
fn test_wyrand_intn() {
max := 2525642
for seed in seeds {
rng := rand.WyRandRNG{}
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
@ -213,7 +213,7 @@ fn test_wyrand_intn() {
fn test_wyrand_i64n() {
max := i64(3246727724653636)
for seed in seeds {
rng := rand.WyRandRNG{}
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
@ -227,7 +227,7 @@ fn test_wyrand_int_in_range() {
min := -4252
max := 1034
for seed in seeds {
rng := rand.WyRandRNG{}
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
@ -241,7 +241,7 @@ fn test_wyrand_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
rng := rand.WyRandRNG{}
mut rng := rand.WyRandRNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)