RandomSeed trait for seeding Rand things from system time

This commit is contained in:
Evan Richter 2021-02-25 11:12:53 -06:00
parent 82a6936154
commit 6e6ac27791
No known key found for this signature in database
GPG Key ID: CCB6AE3EF39EB1AC

View File

@ -71,6 +71,30 @@ where
}
}
#[cfg(feature = "std")]
mod random_seed {
use super::*;
pub trait RandomSeed: Rand + Default {
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
/// Needs stdlib timer
fn with_random_seed() -> Self {
let mut rng = Self::default();
rng.set_seed(current_nanos());
rng
}
}
impl RandomSeed for Xoshiro256StarRand {}
impl RandomSeed for XorShift64Rand {}
impl RandomSeed for Lehmer64Rand {}
impl RandomSeed for RomuTrioRand {}
impl RandomSeed for RomuDuoJrRand {}
}
#[cfg(feature = "std")]
pub use random_seed::*;
const HASH_CONST: u64 = 0xa5b35705;
const DEFAULT_SEED: u64 = 0x54d3a3130133750b;
@ -149,13 +173,6 @@ impl Xoshiro256StarRand {
ret.set_seed(seed); // TODO: Proper random seed?
ret
}
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
/// Needs stdlib timer
#[cfg(feature = "std")]
pub fn preseeded() -> Self {
Self::new(current_nanos())
}
}
/// XXH3 Based, hopefully speedy, rnd implementation
@ -196,13 +213,6 @@ impl XorShift64Rand {
ret.set_seed(seed); // TODO: Proper random seed?
ret
}
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
/// Needs stdlib timer
#[cfg(feature = "std")]
pub fn preseeded() -> Self {
Self::new(current_nanos())
}
}
/// XXH3 Based, hopefully speedy, rnd implementation
@ -246,13 +256,6 @@ impl Lehmer64Rand {
ret.set_seed(seed);
ret
}
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
/// Needs stdlib timer
#[cfg(feature = "std")]
pub fn preseeded() -> Self {
Self::new(current_nanos())
}
}
/// Extremely quick rand implementation
@ -270,13 +273,6 @@ impl RomuTrioRand {
rand.set_seed(seed);
rand
}
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
/// Needs stdlib timer
#[cfg(feature = "std")]
pub fn preseeded() -> Self {
Self::new(current_nanos())
}
}
impl Rand for RomuTrioRand {
@ -324,13 +320,6 @@ impl RomuDuoJrRand {
rand.set_seed(seed);
rand
}
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
/// Needs stdlib timer
#[cfg(feature = "std")]
pub fn preseeded() -> Self {
Self::new(current_nanos())
}
}
impl Rand for RomuDuoJrRand {
@ -401,7 +390,7 @@ impl XKCDRand {
mod tests {
//use xxhash_rust::xxh3::xxh3_64_with_seed;
use crate::utils::{Rand, StdRand};
use crate::utils::{Rand, StdRand, RandomSeed};
#[test]
fn test_rand() {
@ -415,9 +404,9 @@ mod tests {
#[cfg(feature = "std")]
#[test]
fn test_rand_preseeded() {
fn test_rand_with_random_seed() {
let mut rand_fixed = StdRand::new(0);
let mut rand = StdRand::preseeded();
let mut rand = StdRand::with_random_seed();
assert_ne!(rand.next(), rand_fixed.next());
assert_ne!(rand.next(), rand.next());
assert!(rand.below(100) < 100);