2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 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
|
|
|
|
2021-01-26 16:55:09 +03:00
|
|
|
import hash
|
2022-05-22 13:21:52 +03:00
|
|
|
import rand.buffer
|
2022-02-28 14:17:54 +03:00
|
|
|
import rand.seed
|
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)
|
|
|
|
)
|
|
|
|
|
2022-04-07 16:00:30 +03:00
|
|
|
pub const seed_len = 2
|
|
|
|
|
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 {
|
2022-05-22 13:21:52 +03:00
|
|
|
buffer.PRNGBuffer
|
2020-06-01 22:13:56 +03:00
|
|
|
mut:
|
2022-02-28 14:17:54 +03:00
|
|
|
state u64 = seed.time_seed_64()
|
|
|
|
bytes_left int
|
|
|
|
buffer u64
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2022-02-28 14:17:54 +03:00
|
|
|
rng.bytes_left = 0
|
|
|
|
rng.buffer = 0
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2022-02-28 14:17:54 +03:00
|
|
|
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
|
|
|
|
[inline]
|
2022-04-15 18:25:45 +03:00
|
|
|
pub fn (mut rng WyRandRNG) u8() u8 {
|
2022-02-28 14:17:54 +03:00
|
|
|
// Can we extract a value from the buffer?
|
|
|
|
if rng.bytes_left >= 1 {
|
|
|
|
rng.bytes_left -= 1
|
2022-04-15 14:45:52 +03:00
|
|
|
value := u8(rng.buffer)
|
2022-02-28 14:17:54 +03:00
|
|
|
rng.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
// Add a new value to the buffer
|
|
|
|
rng.buffer = rng.u64()
|
|
|
|
rng.bytes_left = 7
|
2022-04-15 14:45:52 +03:00
|
|
|
value := u8(rng.buffer)
|
2022-02-28 14:17:54 +03:00
|
|
|
rng.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// u16 returns a pseudorandom 16bit int in range `[0, 2¹⁶)`.
|
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) u16() u16 {
|
|
|
|
if rng.bytes_left >= 2 {
|
|
|
|
rng.bytes_left -= 2
|
|
|
|
value := u16(rng.buffer)
|
|
|
|
rng.buffer >>= 16
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
ans := rng.u64()
|
|
|
|
rng.buffer = ans >> 16
|
|
|
|
rng.bytes_left = 6
|
|
|
|
return u16(ans)
|
|
|
|
}
|
|
|
|
|
|
|
|
// u32 returns a pseudorandom 32bit int in range `[0, 2³²)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) u32() u32 {
|
2022-02-28 14:17:54 +03:00
|
|
|
if rng.bytes_left >= 4 {
|
|
|
|
rng.bytes_left -= 4
|
|
|
|
value := u32(rng.buffer)
|
|
|
|
rng.buffer >>= 32
|
|
|
|
return value
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
2022-02-28 14:17:54 +03:00
|
|
|
ans := rng.u64()
|
|
|
|
rng.buffer = ans >> 32
|
|
|
|
rng.bytes_left = 4
|
|
|
|
return u32(ans)
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2022-02-28 14:17:54 +03:00
|
|
|
// u64 returns a pseudorandom 64bit int in range `[0, 2⁶⁴)`.
|
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
|
|
|
|
}
|
2022-02-28 14:17:54 +03:00
|
|
|
|
|
|
|
// block_size returns the number of bits that the RNG can produce in a single iteration.
|
|
|
|
[inline]
|
|
|
|
pub fn (mut rng WyRandRNG) block_size() int {
|
|
|
|
return 64
|
|
|
|
}
|