moved new -> with_seed, with_random_seed -> new
This commit is contained in:
parent
776349eaa7
commit
15a64c3e17
@ -85,7 +85,7 @@ fn fuzz(corpus_dirs: Vec<PathBuf>, objective_dir: PathBuf, broker_port: u16) ->
|
|||||||
let mut state = state.unwrap_or_else(|| {
|
let mut state = state.unwrap_or_else(|| {
|
||||||
State::new(
|
State::new(
|
||||||
// RNG
|
// RNG
|
||||||
StdRand::new(current_nanos()),
|
StdRand::with_seed(current_nanos()),
|
||||||
// Corpus that will be evolved, we keep it in memory for performance
|
// Corpus that will be evolved, we keep it in memory for performance
|
||||||
InMemoryCorpus::new(),
|
InMemoryCorpus::new(),
|
||||||
// Feedbacks to rate the interestingness of an input
|
// Feedbacks to rate the interestingness of an input
|
||||||
|
@ -74,7 +74,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_input() {
|
fn test_input() {
|
||||||
let mut rand = StdRand::new(0);
|
let mut rand = StdRand::with_seed(0);
|
||||||
assert_ne!(rand.next(), rand.next());
|
assert_ne!(rand.next(), rand.next());
|
||||||
assert!(rand.below(100) < 100);
|
assert!(rand.below(100) < 100);
|
||||||
assert_eq!(rand.below(1), 0);
|
assert_eq!(rand.below(1), 0);
|
||||||
|
@ -144,7 +144,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fuzzer() {
|
fn test_fuzzer() {
|
||||||
let rand = StdRand::new(0);
|
let rand = StdRand::with_seed(0);
|
||||||
|
|
||||||
let mut corpus = InMemoryCorpus::<BytesInput>::new();
|
let mut corpus = InMemoryCorpus::<BytesInput>::new();
|
||||||
let testcase = Testcase::new(vec![0; 4]).into();
|
let testcase = Testcase::new(vec![0; 4]).into();
|
||||||
|
@ -921,7 +921,7 @@ token2="B"
|
|||||||
BytesInput::new(vec![1; 4]),
|
BytesInput::new(vec![1; 4]),
|
||||||
];
|
];
|
||||||
|
|
||||||
let rand = StdRand::new(1337);
|
let rand = StdRand::with_seed(1337);
|
||||||
let mut corpus = InMemoryCorpus::new();
|
let mut corpus = InMemoryCorpus::new();
|
||||||
|
|
||||||
corpus
|
corpus
|
||||||
|
@ -10,6 +10,8 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||||||
pub type StdRand = RomuTrioRand;
|
pub type StdRand = RomuTrioRand;
|
||||||
|
|
||||||
/// Ways to get random around here
|
/// Ways to get random around here
|
||||||
|
/// Please note that these are not cryptographically secure
|
||||||
|
/// Or, even if some might be by accident, at least they are not seeded in a cryptographically secure fashion.
|
||||||
pub trait Rand: Debug + Serialize + DeserializeOwned {
|
pub trait Rand: Debug + Serialize + DeserializeOwned {
|
||||||
/// Sets the seed of this Rand
|
/// Sets the seed of this Rand
|
||||||
fn set_seed(&mut self, seed: u64);
|
fn set_seed(&mut self, seed: u64);
|
||||||
@ -74,12 +76,17 @@ where
|
|||||||
// helper macro for deriving Default
|
// helper macro for deriving Default
|
||||||
macro_rules! default_rand {
|
macro_rules! default_rand {
|
||||||
($rand: ty) => {
|
($rand: ty) => {
|
||||||
/// A default RNG will produce a deterministic and reproducible stream of random numbers.
|
/// A default RNG will usually produce a nondeterministic stream of random numbers.
|
||||||
/// Use [`RandomSeed::with_random_seed`] to generate unique RNG seeded from some source of
|
/// As we do not have any way to get random seeds for no_std, they have to be reproducible there.
|
||||||
/// randomness.
|
/// Use [`RandomSeed::with_seed`] to generate a reproducible RNG.
|
||||||
impl core::default::Default for $rand {
|
impl core::default::Default for $rand {
|
||||||
|
#[cfg(feature = "std")]
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(DEFAULT_SEED)
|
Self::new()
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::with_seed(0xAF1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -97,7 +104,7 @@ default_rand!(RomuDuoJrRand);
|
|||||||
/// Default implementations are provided with the "std" feature enabled, using system time in
|
/// Default implementations are provided with the "std" feature enabled, using system time in
|
||||||
/// nanoseconds as the initial seed.
|
/// nanoseconds as the initial seed.
|
||||||
pub trait RandomSeed: Rand + Default {
|
pub trait RandomSeed: Rand + Default {
|
||||||
fn with_random_seed() -> Self;
|
fn new() -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper macro to impl RandomSeed
|
// helper macro to impl RandomSeed
|
||||||
@ -106,10 +113,8 @@ macro_rules! impl_randomseed {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl RandomSeed for $rand {
|
impl RandomSeed for $rand {
|
||||||
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
|
/// Creates a rand instance, pre-seeded with the current time in nanoseconds.
|
||||||
fn with_random_seed() -> Self {
|
fn new() -> Self {
|
||||||
let mut rng = Self::default();
|
Self::with_seed(current_nanos())
|
||||||
rng.set_seed(current_nanos());
|
|
||||||
rng
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -122,7 +127,6 @@ impl_randomseed!(RomuTrioRand);
|
|||||||
impl_randomseed!(RomuDuoJrRand);
|
impl_randomseed!(RomuDuoJrRand);
|
||||||
|
|
||||||
const HASH_CONST: u64 = 0xa5b35705;
|
const HASH_CONST: u64 = 0xa5b35705;
|
||||||
const DEFAULT_SEED: u64 = 0x54d3a3130133750b;
|
|
||||||
|
|
||||||
/// Current time
|
/// Current time
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
@ -201,7 +205,7 @@ impl Rand for Xoshiro256StarRand {
|
|||||||
|
|
||||||
impl Xoshiro256StarRand {
|
impl Xoshiro256StarRand {
|
||||||
/// Creates a new Xoshiro rand with the given seed
|
/// Creates a new Xoshiro rand with the given seed
|
||||||
pub fn new(seed: u64) -> Self {
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
let mut rand = Self { rand_seed: [0; 4] };
|
let mut rand = Self { rand_seed: [0; 4] };
|
||||||
rand.set_seed(seed); // TODO: Proper random seed?
|
rand.set_seed(seed); // TODO: Proper random seed?
|
||||||
rand
|
rand
|
||||||
@ -232,7 +236,7 @@ impl Rand for XorShift64Rand {
|
|||||||
|
|
||||||
impl XorShift64Rand {
|
impl XorShift64Rand {
|
||||||
/// Creates a new Xoshiro rand with the given seed
|
/// Creates a new Xoshiro rand with the given seed
|
||||||
pub fn new(seed: u64) -> Self {
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
let mut ret: Self = Self { rand_seed: 0 };
|
let mut ret: Self = Self { rand_seed: 0 };
|
||||||
ret.set_seed(seed); // TODO: Proper random seed?
|
ret.set_seed(seed); // TODO: Proper random seed?
|
||||||
ret
|
ret
|
||||||
@ -259,7 +263,7 @@ impl Rand for Lehmer64Rand {
|
|||||||
|
|
||||||
impl Lehmer64Rand {
|
impl Lehmer64Rand {
|
||||||
/// Creates a new Lehmer rand with the given seed
|
/// Creates a new Lehmer rand with the given seed
|
||||||
pub fn new(seed: u64) -> Self {
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
let mut ret: Self = Self { rand_seed: 0 };
|
let mut ret: Self = Self { rand_seed: 0 };
|
||||||
ret.set_seed(seed);
|
ret.set_seed(seed);
|
||||||
ret
|
ret
|
||||||
@ -276,7 +280,7 @@ pub struct RomuTrioRand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RomuTrioRand {
|
impl RomuTrioRand {
|
||||||
pub fn new(seed: u64) -> Self {
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
let mut rand = Self {
|
let mut rand = Self {
|
||||||
x_state: 0,
|
x_state: 0,
|
||||||
y_state: 0,
|
y_state: 0,
|
||||||
@ -314,7 +318,7 @@ pub struct RomuDuoJrRand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RomuDuoJrRand {
|
impl RomuDuoJrRand {
|
||||||
pub fn new(seed: u64) -> Self {
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
let mut rand = Self {
|
let mut rand = Self {
|
||||||
x_state: 0,
|
x_state: 0,
|
||||||
y_state: 0,
|
y_state: 0,
|
||||||
@ -357,10 +361,15 @@ impl Rand for XKCDRand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A test rng that will return the same value (chose by fair dice roll) for testing.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
impl XKCDRand {
|
impl XKCDRand {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { val: 4 }
|
Self::with_seed(4)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
|
Self { val: seed }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,11 +377,9 @@ impl XKCDRand {
|
|||||||
mod tests {
|
mod tests {
|
||||||
//use xxhash_rust::xxh3::xxh3_64_with_seed;
|
//use xxhash_rust::xxh3::xxh3_64_with_seed;
|
||||||
|
|
||||||
use crate::utils::{Rand, StdRand};
|
use crate::utils::{Rand, *};
|
||||||
|
|
||||||
#[test]
|
fn test_single_rand<R: Rand>(rand: &mut R) {
|
||||||
fn test_rand() {
|
|
||||||
let mut rand = StdRand::new(0);
|
|
||||||
assert_ne!(rand.next(), rand.next());
|
assert_ne!(rand.next(), rand.next());
|
||||||
assert!(rand.below(100) < 100);
|
assert!(rand.below(100) < 100);
|
||||||
assert_eq!(rand.below(1), 0);
|
assert_eq!(rand.below(1), 0);
|
||||||
@ -380,21 +387,26 @@ mod tests {
|
|||||||
assert!(rand.between(11, 20) > 10);
|
assert!(rand.between(11, 20) > 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rands() {
|
||||||
|
// see cargo bench for speed comparisons
|
||||||
|
test_single_rand(&mut StdRand::with_seed(0));
|
||||||
|
test_single_rand(&mut RomuTrioRand::with_seed(0));
|
||||||
|
test_single_rand(&mut RomuDuoJrRand::with_seed(0));
|
||||||
|
test_single_rand(&mut XorShift64Rand::with_seed(0));
|
||||||
|
test_single_rand(&mut Xoshiro256StarRand::with_seed(0));
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[test]
|
#[test]
|
||||||
fn with_random_seed() {
|
fn test_random_seed() {
|
||||||
use crate::utils::RandomSeed;
|
use crate::utils::RandomSeed;
|
||||||
|
|
||||||
let mut rand_fixed = StdRand::new(0);
|
let mut rand_fixed = StdRand::with_seed(0);
|
||||||
let mut rand = StdRand::with_random_seed();
|
let mut rand = StdRand::new();
|
||||||
|
|
||||||
for _ in 0..10000 {
|
// The seed should be reasonably random so these never fail
|
||||||
assert_ne!(rand.next(), rand_fixed.next());
|
assert_ne!(rand.next(), rand_fixed.next());
|
||||||
assert_ne!(rand.next(), rand.next());
|
test_single_rand(&mut rand);
|
||||||
assert!(rand.below(100) < 100);
|
|
||||||
assert_eq!(rand.below(1), 0);
|
|
||||||
assert_eq!(rand.between(10, 10), 10);
|
|
||||||
assert!(rand.between(11, 20) > 10);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user