2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-06-01 22:13:56 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-06-09 16:06:07 +03:00
|
|
|
module wyrand
|
2020-06-01 22:13:56 +03:00
|
|
|
|
|
|
|
import math.bits
|
2021-01-26 16:55:09 +03:00
|
|
|
import rand.seed
|
2021-01-29 12:57:30 +03:00
|
|
|
import rand.constants
|
2021-01-26 16:55:09 +03:00
|
|
|
import hash
|
2020-06-01 22:13:56 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// Redefinition of some constants that we will need for pseudorandom number generation.
|
2020-06-01 22:13:56 +03:00
|
|
|
const (
|
|
|
|
wyp0 = u64(0xa0761d6478bd642f)
|
|
|
|
wyp1 = u64(0xe7037ed1a0b428db)
|
|
|
|
)
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// WyRandRNG is a RNG based on the WyHash hashing algorithm.
|
2020-06-01 22:13:56 +03:00
|
|
|
pub struct WyRandRNG {
|
|
|
|
mut:
|
2021-01-26 16:55:09 +03:00
|
|
|
state u64 = seed.time_seed_64()
|
2020-09-09 16:34:41 +03:00
|
|
|
has_extra bool
|
2020-06-01 22:13:56 +03:00
|
|
|
extra u32
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// seed sets the seed, needs only two `u32`s in little-endian format as [lower, higher].
|
2020-06-01 22:13:56 +03:00
|
|
|
pub fn (mut rng WyRandRNG) seed(seed_data []u32) {
|
|
|
|
if seed_data.len != 2 {
|
|
|
|
eprintln('WyRandRNG needs 2 32-bit unsigned integers as the seed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
rng.state = seed_data[0] | (u64(seed_data[1]) << 32)
|
|
|
|
rng.has_extra = false
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32 updates the PRNG state and returns the next pseudorandom `u32`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) u32() u32 {
|
|
|
|
if rng.has_extra {
|
|
|
|
rng.has_extra = false
|
|
|
|
return rng.extra
|
|
|
|
}
|
|
|
|
full_value := rng.u64()
|
2021-01-29 12:57:30 +03:00
|
|
|
lower := u32(full_value & constants.lower_mask)
|
2020-06-01 22:13:56 +03:00
|
|
|
upper := u32(full_value >> 32)
|
|
|
|
rng.extra = upper
|
|
|
|
rng.has_extra = true
|
|
|
|
return lower
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64 updates the PRNG state and returns the next pseudorandom `u64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) u64() u64 {
|
|
|
|
unsafe {
|
|
|
|
mut seed1 := rng.state
|
2021-01-26 16:55:09 +03:00
|
|
|
seed1 += wyrand.wyp0
|
2020-06-01 22:13:56 +03:00
|
|
|
rng.state = seed1
|
2021-01-26 16:55:09 +03:00
|
|
|
return hash.wymum(seed1 ^ wyrand.wyp1, seed1)
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32n returns a pseudorandom `u32` less than `max`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) u32n(max u32) u32 {
|
|
|
|
if max == 0 {
|
|
|
|
eprintln('max must be positive integer')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
// Check SysRNG in system_rng.c.v for explanation
|
|
|
|
bit_len := bits.len_32(max)
|
|
|
|
if bit_len == 32 {
|
|
|
|
for {
|
|
|
|
value := rng.u32()
|
|
|
|
if value < max {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mask := (u32(1) << (bit_len + 1)) - 1
|
|
|
|
for {
|
|
|
|
value := rng.u32() & mask
|
|
|
|
if value < max {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u32(0)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64n returns a pseudorandom `u64` less than `max`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) u64n(max u64) u64 {
|
|
|
|
if max == 0 {
|
|
|
|
eprintln('max must be positive integer')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
bit_len := bits.len_64(max)
|
|
|
|
if bit_len == 64 {
|
|
|
|
for {
|
|
|
|
value := rng.u64()
|
|
|
|
if value < max {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mask := (u64(1) << (bit_len + 1)) - 1
|
|
|
|
for {
|
|
|
|
value := rng.u64() & mask
|
|
|
|
if value < max {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u64(0)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32n returns a pseudorandom `u32` value that is guaranteed to be in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn (mut rng WyRandRNG) u32_in_range(min u32, max u32) u32 {
|
2020-06-01 22:13:56 +03:00
|
|
|
if max <= min {
|
|
|
|
eprintln('max must be greater than min')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return min + rng.u32n(max - min)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64n returns a pseudorandom `u64` value that is guaranteed to be in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn (mut rng WyRandRNG) u64_in_range(min u64, max u64) u64 {
|
2020-06-01 22:13:56 +03:00
|
|
|
if max <= min {
|
|
|
|
eprintln('max must be greater than min')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return min + rng.u64n(max - min)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int returns a (possibly negative) pseudorandom 32-bit `int`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) int() int {
|
|
|
|
return int(rng.u32())
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64 returns a (possibly negative) pseudorandom 64-bit `i64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) i64() i64 {
|
|
|
|
return i64(rng.u64())
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int31 returns a positive pseudorandom 31-bit `int`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) int31() int {
|
2021-01-29 12:57:30 +03:00
|
|
|
return int(rng.u32() & constants.u31_mask) // Set the 32nd bit to 0.
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int63 returns a positive pseudorandom 63-bit `i64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) int63() i64 {
|
2021-01-29 12:57:30 +03:00
|
|
|
return i64(rng.u64() & constants.u63_mask) // Set the 64th bit to 0.
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// intn returns a pseudorandom `int` in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) intn(max int) int {
|
|
|
|
if max <= 0 {
|
|
|
|
eprintln('max has to be positive.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-02 07:39:38 +03:00
|
|
|
return int(rng.u32n(u32(max)))
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64n returns a pseudorandom int that lies in `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) i64n(max i64) i64 {
|
|
|
|
if max <= 0 {
|
|
|
|
eprintln('max has to be positive.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-02 07:39:38 +03:00
|
|
|
return i64(rng.u64n(u64(max)))
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int_in_range returns a pseudorandom `int` in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn (mut rng WyRandRNG) int_in_range(min int, max int) int {
|
2020-06-01 22:13:56 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64_in_range returns a pseudorandom `i64` in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn (mut rng WyRandRNG) i64_in_range(min i64, max i64) i64 {
|
2020-06-01 22:13:56 +03:00
|
|
|
if max <= min {
|
|
|
|
eprintln('max must be greater than min')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return min + rng.i64n(max - min)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f32 returns a pseudorandom `f32` value in range `[0, 1)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) f32() f32 {
|
2021-01-29 12:57:30 +03:00
|
|
|
return f32(rng.u32()) / constants.max_u32_as_f32
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f64 returns a pseudorandom `f64` value in range `[0, 1)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) f64() f64 {
|
2021-01-29 12:57:30 +03:00
|
|
|
return f64(rng.u64()) / constants.max_u64_as_f64
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f32n returns a pseudorandom `f32` value in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) f32n(max f32) f32 {
|
|
|
|
if max <= 0 {
|
|
|
|
eprintln('max has to be positive.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return rng.f32() * max
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f64n returns a pseudorandom `f64` value in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) f64n(max f64) f64 {
|
|
|
|
if max <= 0 {
|
|
|
|
eprintln('max has to be positive.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return rng.f64() * max
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f32_in_range returns a pseudorandom `f32` in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn (mut rng WyRandRNG) f32_in_range(min f32, max f32) f32 {
|
2020-06-01 22:13:56 +03:00
|
|
|
if max <= min {
|
|
|
|
eprintln('max must be greater than min')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return min + rng.f32n(max - min)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64_in_range returns a pseudorandom `i64` in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn (mut rng WyRandRNG) f64_in_range(min f64, max f64) f64 {
|
2020-06-01 22:13:56 +03:00
|
|
|
if max <= min {
|
|
|
|
eprintln('max must be greater than min')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return min + rng.f64n(max - min)
|
|
|
|
}
|