Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rand_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Bump the MSRV to 1.61.0
- The `serde1` feature has been renamed `serde` (#1477)
- Implement `SeedableRng` for `StepRng`

## [0.9.0-alpha.1] - 2024-03-18

Expand Down
15 changes: 14 additions & 1 deletion src/rngs/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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);
Copy link
Member

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 use u64::from_le (or u64::from_le_bytes with as_chunks) here. Note that rand_core::impls::fill_bytes_via_next uses to_le_bytes so this would cancel out with a word-based-generator on a BE platform.

StepRng {
v: seed_u64[0],
a: seed_u64[1],
Copy link
Member

Choose a reason for hiding this comment

The 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 seed_u64[1] | 1.

Copy link
Collaborator Author

@benjamin-lieser benjamin-lieser Sep 23, 2024

Choose a reason for hiding this comment

The 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 StepRng are probably the easy predictability and SeedableRng goes against this

Copy link
Member

Choose a reason for hiding this comment

The 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)]
Expand Down