2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-06-01 21:13:56 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-06-09 15:06:07 +02:00
|
|
|
module pcg32
|
|
|
|
|
2021-01-26 14:55:09 +01:00
|
|
|
import rand.seed
|
2022-05-22 12:21:52 +02:00
|
|
|
import rand.buffer
|
2022-02-28 12:17:54 +01:00
|
|
|
|
|
|
|
pub const seed_len = 4
|
2020-06-01 21:13:56 +02:00
|
|
|
|
2020-12-27 19:06:17 +01:00
|
|
|
// PCG32RNG ported from http://www.pcg-random.org/download.html,
|
2020-06-01 21:13:56 +02: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 {
|
2022-05-22 12:21:52 +02:00
|
|
|
buffer.PRNGBuffer
|
2019-09-22 23:50:22 +02:00
|
|
|
mut:
|
2022-05-22 12:21:52 +02:00
|
|
|
state u64 = u64(0x853c49e6748fea9b) ^ seed.time_seed_64()
|
|
|
|
inc u64 = u64(0xda3e39cb94b95bdb) ^ seed.time_seed_64()
|
2020-06-01 21:13:56 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2020-12-27 19:06:17 +01: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 21:13:56 +02: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 19:06:17 +01: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 21:13:56 +02:00
|
|
|
exit(1)
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
2020-06-01 21:13:56 +02: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-22 23:50:22 +02:00
|
|
|
rng.state = u64(0)
|
2020-06-01 21:13:56 +02:00
|
|
|
rng.inc = (init_seq << u64(1)) | u64(1)
|
|
|
|
rng.u32()
|
|
|
|
rng.state += init_state
|
|
|
|
rng.u32()
|
2022-02-28 12:17:54 +01:00
|
|
|
rng.bytes_left = 0
|
|
|
|
rng.buffer = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
|
|
|
|
[inline]
|
2022-04-15 17:25:45 +02:00
|
|
|
pub fn (mut rng PCG32RNG) u8() u8 {
|
2022-02-28 12:17:54 +01:00
|
|
|
if rng.bytes_left >= 1 {
|
|
|
|
rng.bytes_left -= 1
|
2022-04-15 13:58:56 +02:00
|
|
|
value := u8(rng.buffer)
|
2022-02-28 12:17:54 +01:00
|
|
|
rng.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
rng.buffer = rng.u32()
|
|
|
|
rng.bytes_left = 3
|
2022-04-15 13:58:56 +02:00
|
|
|
value := u8(rng.buffer)
|
2022-02-28 12:17:54 +01:00
|
|
|
rng.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// u16 returns a pseudorandom 16-bit unsigned integer (`u16`).
|
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) u16() u16 {
|
|
|
|
if rng.bytes_left >= 2 {
|
|
|
|
rng.bytes_left -= 2
|
|
|
|
value := u16(rng.buffer)
|
|
|
|
rng.buffer >>= 16
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
ans := rng.u32()
|
|
|
|
rng.buffer = ans >> 16
|
|
|
|
rng.bytes_left = 2
|
|
|
|
return u16(ans)
|
2019-09-22 23:50:22 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2020-12-27 19:06:17 +01:00
|
|
|
// u32 returns a pseudorandom unsigned `u32`.
|
2019-12-19 22:29:37 +01:00
|
|
|
[inline]
|
2022-04-10 10:43:46 +02:00
|
|
|
pub fn (mut rng PCG32RNG) u32() u32 {
|
2019-09-22 23:50:22 +02:00
|
|
|
oldstate := rng.state
|
2019-12-07 13:51:00 +01:00
|
|
|
rng.state = oldstate * (6364136223846793005) + rng.inc
|
2020-06-01 21:13:56 +02:00
|
|
|
xorshifted := u32(((oldstate >> u64(18)) ^ oldstate) >> u64(27))
|
|
|
|
rot := u32(oldstate >> u64(59))
|
2021-09-08 13:19:53 +02:00
|
|
|
return (xorshifted >> rot) | (xorshifted << ((-rot) & u32(31)))
|
2019-09-22 23:50:22 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2020-12-27 19:06:17 +01:00
|
|
|
// u64 returns a pseudorandom 64-bit unsigned `u64`.
|
2020-06-01 21:13:56 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) u64() u64 {
|
|
|
|
return u64(rng.u32()) | (u64(rng.u32()) << 32)
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2022-02-28 12:17:54 +01:00
|
|
|
// block_size returns the number of bits that the RNG can produce in a single iteration.
|
|
|
|
[inline]
|
|
|
|
pub fn (mut rng PCG32RNG) block_size() int {
|
|
|
|
return 32
|
|
|
|
}
|
|
|
|
|
2021-09-23 10:14:20 +02:00
|
|
|
// free should be called when the generator is no longer needed
|
|
|
|
[unsafe]
|
|
|
|
pub fn (mut rng PCG32RNG) free() {
|
|
|
|
unsafe { free(rng) }
|
|
|
|
}
|