[core] Random Number Generator
Implement a random number generator as a thin wrapper around rand::StdRng. Provide some default seed data to get consistent random numbers.
This commit is contained in:
parent
f47654cc98
commit
6d0df32f74
4 changed files with 143 additions and 2 deletions
|
@ -6,4 +6,5 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.9.1"
|
||||
thiserror = "2"
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
pub mod colors;
|
||||
pub mod coordinates;
|
||||
pub mod pieces;
|
||||
pub mod random;
|
||||
pub mod shapes;
|
||||
|
||||
mod macros;
|
||||
|
|
44
core/src/random.rs
Normal file
44
core/src/random.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
// 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 }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue