2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 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 pcg32
|
|
|
|
|
2021-01-26 16:55:09 +03:00
|
|
|
import rand.seed
|
2021-01-29 12:57:30 +03:00
|
|
|
import rand.constants
|
2020-06-01 22:13:56 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// PCG32RNG ported from http://www.pcg-random.org/download.html,
|
2020-06-01 22:13:56 +03:00
|
|
|
// https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c, and
|
|
|
|
// https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.h
|
|
|
|
pub struct PCG32RNG {
|
2019-09-23 00:50:22 +03:00
|
|
|
mut:
|
2021-01-26 16:55:09 +03:00
|
|
|
state u64 = u64(0x853c49e6748fea9b) ^ seed.time_seed_64()
|
|
|
|
inc u64 = u64(0xda3e39cb94b95bdb) ^ seed.time_seed_64()
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// seed seeds the PCG32RNG with 4 `u32` values.
|
|
|
|
// The first 2 represent the 64-bit initial state as `[lower 32 bits, higher 32 bits]`
|
2020-06-01 22:13:56 +03:00
|
|
|
// The last 2 represent the 64-bit stream/step of the PRNG.
|
|
|
|
pub fn (mut rng PCG32RNG) seed(seed_data []u32) {
|
|
|
|
if seed_data.len != 4 {
|
2020-12-27 21:06:17 +03:00
|
|
|
eprintln('PCG32RNG needs 4 u32s to be seeded. First two the initial state and the last two the stream/step. Both in little endian format: [lower, higher].')
|
2020-06-01 22:13:56 +03:00
|
|
|
exit(1)
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2020-06-01 22:13:56 +03:00
|
|
|
init_state := u64(seed_data[0]) | (u64(seed_data[1]) << 32)
|
|
|
|
init_seq := u64(seed_data[2]) | (u64(seed_data[3]) << 32)
|
2019-09-23 00:50:22 +03:00
|
|
|
rng.state = u64(0)
|
2020-06-01 22:13:56 +03:00
|
|
|
rng.inc = (init_seq << u64(1)) | u64(1)
|
|
|
|
rng.u32()
|
|
|
|
rng.state += init_state
|
|
|
|
rng.u32()
|
2019-09-23 00:50:22 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32 returns a pseudorandom unsigned `u32`.
|
2019-12-20 00:29:37 +03:00
|
|
|
[inline]
|
2020-06-01 22:13:56 +03:00
|
|
|
pub fn (mut rng PCG32RNG) u32() u32 {
|
2019-09-23 00:50:22 +03:00
|
|
|
oldstate := rng.state
|
2019-12-07 15:51:00 +03:00
|
|
|
rng.state = oldstate * (6364136223846793005) + rng.inc
|
2020-06-01 22:13:56 +03:00
|
|
|
xorshifted := u32(((oldstate >> u64(18)) ^ oldstate) >> u64(27))
|
|
|
|
rot := u32(oldstate >> u64(59))
|
|
|
|
return ((xorshifted >> rot) | (xorshifted << ((-rot) & u32(31))))
|
2019-09-23 00:50:22 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64 returns a pseudorandom 64-bit unsigned `u64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) u64() u64 {
|
|
|
|
return u64(rng.u32()) | (u64(rng.u32()) << 32)
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32n returns a pseudorandom 32-bit unsigned `u32` in range `[0, max)`.
|
2019-12-20 00:29:37 +03:00
|
|
|
[inline]
|
2020-06-01 22:13:56 +03:00
|
|
|
pub fn (mut rng PCG32RNG) u32n(max u32) u32 {
|
|
|
|
if max == 0 {
|
|
|
|
eprintln('max must be positive')
|
|
|
|
exit(1)
|
|
|
|
}
|
2019-09-23 00:50:22 +03:00
|
|
|
// To avoid bias, we need to make the range of the RNG a multiple of
|
2020-06-01 22:13:56 +03:00
|
|
|
// max, which we do by dropping output less than a threshold.
|
|
|
|
threshold := (-max % max)
|
2019-09-23 00:50:22 +03:00
|
|
|
// Uniformity guarantees that loop below will terminate. In practice, it
|
2020-06-01 22:13:56 +03:00
|
|
|
// should usually terminate quickly; on average (assuming all max's are
|
2019-09-23 00:50:22 +03:00
|
|
|
// equally likely), 82.25% of the time, we can expect it to require just
|
2020-06-01 22:13:56 +03:00
|
|
|
// one iteration. In practice, max's are typically small and only a
|
2019-09-23 00:50:22 +03:00
|
|
|
// tiny amount of the range is eliminated.
|
|
|
|
for {
|
2020-06-01 22:13:56 +03:00
|
|
|
r := rng.u32()
|
2019-09-23 00:50:22 +03:00
|
|
|
if r >= threshold {
|
2020-06-01 22:13:56 +03:00
|
|
|
return (r % max)
|
2019-12-07 15:51:00 +03:00
|
|
|
}
|
2019-09-23 00:50:22 +03:00
|
|
|
}
|
|
|
|
return u32(0)
|
|
|
|
}
|
2020-06-01 22:13:56 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64n returns a pseudorandom 64-bit unsigned `u64` in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) u64n(max u64) u64 {
|
|
|
|
if max == 0 {
|
|
|
|
eprintln('max must be positive')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
threshold := (-max % max)
|
|
|
|
for {
|
|
|
|
r := rng.u64()
|
|
|
|
if r >= threshold {
|
|
|
|
return (r % max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u64(0)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32_in_range returns a pseudorandom 32-bit unsigned `u32` in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2021-04-21 12:20:55 +03:00
|
|
|
pub fn (mut rng PCG32RNG) 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)
|
|
|
|
}
|
2020-06-02 07:39:38 +03:00
|
|
|
return min + rng.u32n(u32(max - min))
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64_in_range returns a pseudorandom 64-bit unsigned `u64` in range `[min, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-21 12:23:03 +03:00
|
|
|
pub fn (mut rng PCG32RNG) 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 32-bit signed (possibly negative) `int`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) int() int {
|
|
|
|
return int(rng.u32())
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64 returns a 64-bit signed (possibly negative) `i64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) i64() i64 {
|
|
|
|
return i64(rng.u64())
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int31 returns a 31-bit positive pseudorandom `int`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) int31() int {
|
|
|
|
return int(rng.u32() >> 1)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int63 returns a 63-bit positive pseudorandom `i64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) int63() i64 {
|
|
|
|
return i64(rng.u64() >> 1)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// intn returns a 32-bit positive `int` in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) 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 64-bit positive `i64` in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) 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 32-bit positive `int` in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-21 12:23:03 +03:00
|
|
|
pub fn (mut rng PCG32RNG) 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)
|
|
|
|
}
|
|
|
|
return min + rng.intn(max - min)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64_in_range returns a 64-bit positive `i64` in range `[0, max)`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
2020-10-21 12:23:03 +03:00
|
|
|
pub fn (mut rng PCG32RNG) 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 PCG32RNG) 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 PCG32RNG) 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 PCG32RNG) 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 PCG32RNG) 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-21 12:23:03 +03:00
|
|
|
pub fn (mut rng PCG32RNG) 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-21 12:23:03 +03:00
|
|
|
pub fn (mut rng PCG32RNG) 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)
|
|
|
|
}
|