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

Conversation

benjamin-lieser
Copy link
Collaborator

@benjamin-lieser benjamin-lieser commented Sep 23, 2024

  • Added a CHANGELOG.md entry

Summary

implement SeedableRng for StepRng

Motivation

#1495

Details

This is not value stable across endianess, not sure if we need/want that here

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.

let seed_u64: [u64; 2] = transmute!(seed);
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.

@dhardy
Copy link
Member

dhardy commented Sep 23, 2024

Note: StepRng was introduced in #268 where I noted being unsure about SeedableRng. I don't see other discussion of that. I think there shouldn't be an issue with implementing this now.

We should briefly document what this impl does, on the impl itself.

@dhardy
Copy link
Member

dhardy commented Sep 28, 2024

Closing (along with motivating issue).

@dhardy dhardy closed this Sep 28, 2024
@benjamin-lieser benjamin-lieser deleted the seedable_rng branch October 8, 2024 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants