-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
impl SeedableRng for StepRng #1496
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,11 @@ | |
|
||
//! Mock random number generator | ||
|
||
use rand_core::{impls, RngCore}; | ||
use rand_core::{impls, RngCore, SeedableRng}; | ||
|
||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
use zerocopy::transmute; | ||
|
||
/// A mock generator yielding very predictable output | ||
/// | ||
|
@@ -75,6 +76,18 @@ impl RngCore for StepRng { | |
} | ||
} | ||
|
||
impl SeedableRng for StepRng { | ||
type Seed = [u8; 16]; | ||
|
||
fn from_seed(seed: Self::Seed) -> Self { | ||
let seed_u64: [u64; 2] = transmute!(seed); | ||
StepRng { | ||
v: seed_u64[0], | ||
a: seed_u64[1], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concern: if this were to randomly sample a 0, the output would be constant; if it randomly samples an even number, half the output values will be unreachable. I'm not entirely sure what behaviour people would expect here but suggest we do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me think this impl is a bad idea. The usecases for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be that my concern is entirely invalid given that this is a mock RNG, but you may also be right. |
||
} | ||
} | ||
} | ||
|
||
rand_core::impl_try_rng_from_rng_core!(StepRng); | ||
|
||
#[cfg(test)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concern: we should make results portable where (easily enough) possible.
transmute!
presumably does not alter the value, so we would need to useu64::from_le
(oru64::from_le_bytes
withas_chunks
) here. Note thatrand_core::impls::fill_bytes_via_next
usesto_le_bytes
so this would cancel out with a word-based-generator on a BE platform.