dicetest/seed.rs
1use getrandom::getrandom;
2
3/// A seed for pseudorandomness.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct Seed(pub u64);
6
7impl Seed {
8 /// Creates a random seed using the random number generator of the operating system.
9 ///
10 /// # Panics
11 ///
12 /// If the operation system fails to generate the needed amount of random bytes this function
13 /// will panic. See the documentation of [getrandom] for more details.
14 ///
15 /// [getrandom]: https://docs.rs/getrandom
16 pub fn random() -> Self {
17 let mut buf = [0u8; 8];
18 getrandom(&mut buf).expect("Random seed generation has failed");
19 let seed = u64::from_le_bytes(buf);
20 Seed(seed)
21 }
22}
23
24impl From<u64> for Seed {
25 fn from(seed: u64) -> Self {
26 Seed(seed)
27 }
28}