| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module pcg32 |
| 5 | |
| 6 | import rand.seed |
| 7 | import rand.buffer |
| 8 | |
| 9 | pub const seed_len = 4 |
| 10 | |
| 11 | // PCG32RNG is ported from http://www.pcg-random.org/download.html . |
| 12 | // https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c, and |
| 13 | // https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.h |
| 14 | pub struct PCG32RNG { |
| 15 | buffer.PRNGBuffer |
| 16 | mut: |
| 17 | state u64 = u64(0x853c49e6748fea9b) ^ seed.time_seed_64() |
| 18 | inc u64 = (u64(0xda3e39cb94b95bdb) ^ seed.time_seed_64()) | u64(1) |
| 19 | } |
| 20 | |
| 21 | // seed seeds the PCG32RNG with 4 `u32` values. |
| 22 | // The first 2 represent the 64-bit initial state as `[lower 32 bits, higher 32 bits]` |
| 23 | // The last 2 represent the 64-bit stream/step of the PRNG. |
| 24 | @[ignore_overflow] |
| 25 | pub fn (mut rng PCG32RNG) seed(seed_data []u32) { |
| 26 | if seed_data.len != 4 { |
| 27 | 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].') |
| 28 | exit(1) |
| 29 | } |
| 30 | init_state := u64(seed_data[0]) | (u64(seed_data[1]) << 32) |
| 31 | init_seq := u64(seed_data[2]) | (u64(seed_data[3]) << 32) |
| 32 | rng.state = u64(0) |
| 33 | rng.inc = (init_seq << u64(1)) | u64(1) |
| 34 | rng.u32() |
| 35 | rng.state += init_state |
| 36 | rng.u32() |
| 37 | rng.bytes_left = 0 |
| 38 | rng.buffer = 0 |
| 39 | } |
| 40 | |
| 41 | // byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`. |
| 42 | @[inline] |
| 43 | pub fn (mut rng PCG32RNG) u8() u8 { |
| 44 | if rng.bytes_left >= 1 { |
| 45 | rng.bytes_left -= 1 |
| 46 | value := u8(rng.buffer) |
| 47 | rng.buffer >>= 8 |
| 48 | return value |
| 49 | } |
| 50 | rng.buffer = rng.u32() |
| 51 | rng.bytes_left = 3 |
| 52 | value := u8(rng.buffer) |
| 53 | rng.buffer >>= 8 |
| 54 | return value |
| 55 | } |
| 56 | |
| 57 | // u16 returns a pseudorandom 16-bit unsigned integer (`u16`). |
| 58 | @[inline] |
| 59 | pub fn (mut rng PCG32RNG) u16() u16 { |
| 60 | if rng.bytes_left >= 2 { |
| 61 | rng.bytes_left -= 2 |
| 62 | value := u16(rng.buffer) |
| 63 | rng.buffer >>= 16 |
| 64 | return value |
| 65 | } |
| 66 | ans := rng.u32() |
| 67 | rng.buffer = ans >> 16 |
| 68 | rng.bytes_left = 2 |
| 69 | return u16(ans) |
| 70 | } |
| 71 | |
| 72 | // u32 returns a pseudorandom unsigned `u32`. |
| 73 | @[ignore_overflow; inline] |
| 74 | pub fn (mut rng PCG32RNG) u32() u32 { |
| 75 | oldstate := rng.state |
| 76 | rng.state = oldstate * (6364136223846793005) + rng.inc |
| 77 | xorshifted := u32(((oldstate >> u64(18)) ^ oldstate) >> u64(27)) |
| 78 | rot := u32(oldstate >> u64(59)) |
| 79 | return (xorshifted >> rot) | (xorshifted << ((-rot) & u32(31))) |
| 80 | } |
| 81 | |
| 82 | // u64 returns a pseudorandom 64-bit unsigned `u64`. |
| 83 | @[inline] |
| 84 | pub fn (mut rng PCG32RNG) u64() u64 { |
| 85 | return u64(rng.u32()) | (u64(rng.u32()) << 32) |
| 86 | } |
| 87 | |
| 88 | // block_size returns the number of bits that the RNG can produce in a single iteration. |
| 89 | @[inline] |
| 90 | pub fn (mut rng PCG32RNG) block_size() int { |
| 91 | return 32 |
| 92 | } |
| 93 | |
| 94 | // free should be called when the generator is no longer needed. |
| 95 | @[unsafe] |
| 96 | pub fn (mut rng PCG32RNG) free() { |
| 97 | } |
| 98 | |