45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
|
|
// Eryn Wells <eryn@erynwells.me>
|
||
|
|
|
||
|
|
use rand::{rngs::StdRng, RngCore, SeedableRng};
|
||
|
|
|
||
|
|
pub struct RandomNumberGenerator {
|
||
|
|
rng: StdRng,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl RandomNumberGenerator {
|
||
|
|
/// A default value for hashing chess games.
|
||
|
|
///
|
||
|
|
/// This value is the SHA-256 hash of a PGN I downloaded from the internet
|
||
|
|
/// of the six games Kasparov played against Deep Blue in 1996.
|
||
|
|
#[rustfmt::skip]
|
||
|
|
const DEFAULT_SEED_VALUE: [u8; 32] = [
|
||
|
|
0x22, 0x0b, 0xae, 0xd5, 0xc5, 0xb8, 0x20, 0xb1,
|
||
|
|
0xee, 0x05, 0x4b, 0x72, 0x73, 0x4c, 0x1a, 0xf9,
|
||
|
|
0xc2, 0xb7, 0x5d, 0x87, 0xc8, 0xa9, 0x44, 0x87,
|
||
|
|
0x01, 0xda, 0xdb, 0xe0, 0x8e, 0xf8, 0xe8, 0x77,
|
||
|
|
];
|
||
|
|
|
||
|
|
#[must_use]
|
||
|
|
pub fn new(seed: <StdRng as SeedableRng>::Seed) -> Self {
|
||
|
|
let rng = StdRng::from_seed(seed);
|
||
|
|
|
||
|
|
Self { rng }
|
||
|
|
}
|
||
|
|
|
||
|
|
#[must_use]
|
||
|
|
pub fn rand(&mut self) -> &mut StdRng {
|
||
|
|
&mut self.rng
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn next_u64(&mut self) -> u64 {
|
||
|
|
self.rng.next_u64()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Default for RandomNumberGenerator {
|
||
|
|
fn default() -> Self {
|
||
|
|
let rng = StdRng::from_seed(Self::DEFAULT_SEED_VALUE);
|
||
|
|
Self { rng }
|
||
|
|
}
|
||
|
|
}
|