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

rand: simplify rand.PRNG, move to optional types for error handling (#13570)

This commit is contained in:
Subhomoy Haldar
2022-02-23 16:06:14 +05:30
committed by GitHub
parent 5c0b7b0d05
commit 114a341f5f
49 changed files with 609 additions and 1586 deletions

View File

@ -50,180 +50,6 @@ pub fn (mut rng SplitMix64RNG) u64() u64 {
return z ^ (z >> (31))
}
// u32n returns a pseudorandom `u32` less than `bound`.
[inline]
pub fn (mut rng SplitMix64RNG) u32n(bound u32) u32 {
// This function is kept similar to the u64 version
if bound == 0 {
eprintln('max must be non-zero')
exit(1)
}
threshold := -bound % bound
for {
r := rng.u32()
if r >= threshold {
return r % bound
}
}
return u32(0)
}
// u64n returns a pseudorandom `u64` less than `bound`.
[inline]
pub fn (mut rng SplitMix64RNG) u64n(bound u64) u64 {
// See pcg32.v for explanation of comment. This algorithm
// existed before the refactoring.
if bound == 0 {
eprintln('max must be non-zero')
exit(1)
}
threshold := -bound % bound
for {
r := rng.u64()
if r >= threshold {
return r % bound
}
}
return u64(0)
}
// u32n returns a pseudorandom `u32` value that is guaranteed to be in range `[min, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) u32_in_range(min u32, max u32) u32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.u32n(max - min)
}
// u64n returns a pseudorandom `u64` value that is guaranteed to be in range `[min, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.u64n(max - min)
}
// int returns a pseudorandom 32-bit (possibly negative) `int`.
[inline]
pub fn (mut rng SplitMix64RNG) int() int {
return int(rng.u32())
}
// i64 returns a pseudorandom 64-bit (possibly negative) `i64`.
[inline]
pub fn (mut rng SplitMix64RNG) i64() i64 {
return i64(rng.u64())
}
// int31 returns a positive pseudorandom 31-bit `int`.
[inline]
pub fn (mut rng SplitMix64RNG) int31() int {
return int(rng.u32() & constants.u31_mask) // Set the 32nd bit to 0.
}
// int63 returns a positive pseudorandom 63-bit `i64`.
[inline]
pub fn (mut rng SplitMix64RNG) int63() i64 {
return i64(rng.u64() & constants.u63_mask) // Set the 64th bit to 0.
}
// intn returns a pseudorandom `int` in range `[0, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) intn(max int) int {
if max <= 0 {
eprintln('max has to be positive.')
exit(1)
}
return int(rng.u32n(u32(max)))
}
// i64n returns a pseudorandom `i64` in range `[0, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) i64n(max i64) i64 {
if max <= 0 {
eprintln('max has to be positive.')
exit(1)
}
return i64(rng.u64n(u64(max)))
}
// int_in_range returns a pseudorandom `int` in range `[min, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) int_in_range(min int, max int) int {
if max <= min {
eprintln('max must be greater than min')
exit(1)
}
// This supports negative ranges like [-10, -5) because the difference is positive
return min + rng.intn(max - min)
}
// i64_in_range returns a pseudorandom `i64` in range `[min, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.i64n(max - min)
}
// f32 returns a pseudorandom `f32` value in range `[0, 1)`.
[inline]
pub fn (mut rng SplitMix64RNG) f32() f32 {
return f32(rng.u32()) / constants.max_u32_as_f32
}
// f64 returns a pseudorandom `f64` value in range `[0, 1)`.
[inline]
pub fn (mut rng SplitMix64RNG) f64() f64 {
return f64(rng.u64()) / constants.max_u64_as_f64
}
// f32n returns a pseudorandom `f32` value in range `[0, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) f32n(max f32) f32 {
if max <= 0 {
eprintln('max has to be positive.')
exit(1)
}
return rng.f32() * max
}
// f64n returns a pseudorandom `f64` value in range `[0, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) f64n(max f64) f64 {
if max <= 0 {
eprintln('max has to be positive.')
exit(1)
}
return rng.f64() * max
}
// f32_in_range returns a pseudorandom `f32` in range `[min, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.f32n(max - min)
}
// i64_in_range returns a pseudorandom `i64` in range `[min, max)`.
[inline]
pub fn (mut rng SplitMix64RNG) f64_in_range(min f64, max f64) f64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
}
return min + rng.f64n(max - min)
}
// free should be called when the generator is no longer needed
[unsafe]
pub fn (mut rng SplitMix64RNG) free() {

View File

@ -1,4 +1,5 @@
import math
import rand
import rand.splitmix64
import rand.seed
@ -17,10 +18,10 @@ const (
fn gen_randoms(seed_data []u32, bound int) []u64 {
bound_u64 := u64(bound)
mut randoms := []u64{len: (20)}
mut rnd := splitmix64.SplitMix64RNG{}
mut rnd := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rnd.seed(seed_data)
for i in 0 .. 20 {
randoms[i] = rnd.u64n(bound_u64)
randoms[i] = rnd.u64n(bound_u64) or { panic("Couldn't obtain u64") }
}
return randoms
}
@ -36,40 +37,29 @@ fn test_splitmix64_reproducibility() {
}
}
// TODO: use the `in` syntax and remove this function
// after generics has been completely implemented
fn found(value u64, arr []u64) bool {
for item in arr {
if value == item {
return true
}
}
return false
}
fn test_splitmix64_variability() {
// If this test fails and if it is certainly not the implementation
// at fault, try changing the seed values. Repeated values are
// improbable but not impossible.
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
mut values := []u64{cap: value_count}
for i in 0 .. value_count {
value := rng.u64()
assert !found(value, values)
assert value !in values
assert values.len == i
values << value
}
}
}
fn check_uniformity_u64(mut rng splitmix64.SplitMix64RNG, range u64) {
fn check_uniformity_u64(mut rng rand.PRNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
for _ in 0 .. sample_size {
diff := f64(rng.u64n(range)) - expected_mean
diff := f64(rng.u64n(range) or { panic("Couldn't obtain u64") }) - expected_mean
variance += diff * diff
}
variance /= sample_size - 1
@ -82,7 +72,7 @@ fn check_uniformity_u64(mut rng splitmix64.SplitMix64RNG, range u64) {
fn test_splitmix64_uniformity_u64() {
ranges := [14019545, 80240, 130]
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for range in ranges {
check_uniformity_u64(mut rng, u64(range))
@ -90,7 +80,7 @@ fn test_splitmix64_uniformity_u64() {
}
}
fn check_uniformity_f64(mut rng splitmix64.SplitMix64RNG) {
fn check_uniformity_f64(mut rng rand.PRNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
@ -107,7 +97,7 @@ fn check_uniformity_f64(mut rng splitmix64.SplitMix64RNG) {
fn test_splitmix64_uniformity_f64() {
// The f64 version
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
check_uniformity_f64(mut rng)
}
@ -116,10 +106,10 @@ fn test_splitmix64_uniformity_f64() {
fn test_splitmix64_u32n() {
max := u32(16384)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
value := rng.u32n(max) or { panic("Couldn't obtain u32") }
assert value >= 0
assert value < max
}
@ -129,10 +119,10 @@ fn test_splitmix64_u32n() {
fn test_splitmix64_u64n() {
max := u64(379091181005)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u64n(max)
value := rng.u64n(max) or { panic("Couldn't obtain u64") }
assert value >= 0
assert value < max
}
@ -143,10 +133,10 @@ fn test_splitmix64_u32_in_range() {
max := u32(484468466)
min := u32(316846)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(min, max)
value := rng.u32_in_range(min, max) or { panic("Couldn't obtain u32 in range") }
assert value >= min
assert value < max
}
@ -157,10 +147,10 @@ fn test_splitmix64_u64_in_range() {
max := u64(216468454685163)
min := u64(6848646868)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u64_in_range(min, max)
value := rng.u64_in_range(min, max) or { panic("Couldn't obtain u64 in range") }
assert value >= min
assert value < max
}
@ -171,7 +161,7 @@ fn test_splitmix64_int31() {
max_u31 := int(0x7FFFFFFF)
sign_mask := int(0x80000000)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int31()
@ -187,7 +177,7 @@ fn test_splitmix64_int63() {
max_u63 := i64(0x7FFFFFFFFFFFFFFF)
sign_mask := i64(0x8000000000000000)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int63()
@ -201,10 +191,10 @@ fn test_splitmix64_int63() {
fn test_splitmix64_intn() {
max := 2525642
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
value := rng.intn(max) or { panic("Couldn't obtain int") }
assert value >= 0
assert value < max
}
@ -214,10 +204,10 @@ fn test_splitmix64_intn() {
fn test_splitmix64_i64n() {
max := i64(3246727724653636)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
value := rng.i64n(max) or { panic("Couldn't obtain i64") }
assert value >= 0
assert value < max
}
@ -228,10 +218,10 @@ fn test_splitmix64_int_in_range() {
min := -4252
max := 230549862
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
value := rng.int_in_range(min, max) or { panic("Couldn't obtain int in range") }
assert value >= min
assert value < max
}
@ -242,10 +232,10 @@ fn test_splitmix64_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)
value := rng.i64_in_range(min, max) or { panic("Couldn't obtain i64 in range") }
assert value >= min
assert value < max
}
@ -254,7 +244,7 @@ fn test_splitmix64_i64_in_range() {
fn test_splitmix64_f32() {
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f32()
@ -266,7 +256,7 @@ fn test_splitmix64_f32() {
fn test_splitmix64_f64() {
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f64()
@ -279,10 +269,10 @@ fn test_splitmix64_f64() {
fn test_splitmix64_f32n() {
max := f32(357.0)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f32n(max)
value := rng.f32n(max) or { panic("Couldn't obtain f32") }
assert value >= 0.0
assert value < max
}
@ -292,10 +282,10 @@ fn test_splitmix64_f32n() {
fn test_splitmix64_f64n() {
max := 1.52e6
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f64n(max)
value := rng.f64n(max) or { panic("Couldn't obtain f64") }
assert value >= 0.0
assert value < max
}
@ -306,10 +296,10 @@ fn test_splitmix64_f32_in_range() {
min := f32(-24.0)
max := f32(125.0)
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f32_in_range(min, max)
value := rng.f32_in_range(min, max) or { panic("Couldn't obtain f32 in range") }
assert value >= min
assert value < max
}
@ -320,10 +310,10 @@ fn test_splitmix64_f64_in_range() {
min := -548.7
max := 5015.2
for seed in seeds {
mut rng := splitmix64.SplitMix64RNG{}
mut rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f64_in_range(min, max)
value := rng.f64_in_range(min, max) or { panic("Couldn't obtain f64 in range") }
assert value >= min
assert value < max
}