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

SeedableRng cannot be implemented for large RNGs using the recommended seed type #354

Closed
vks opened this issue Mar 29, 2018 · 14 comments
Closed
Labels
P-high Priority: high

Comments

@vks
Copy link
Collaborator

vks commented Mar 29, 2018

For example, xorshift1024* has a recommended [u8; 128] seed type, for which std::convert::AsMut<[u8]> is not implemented, making it impossible to implement SeedableRng. This also affects common RNGs like the Mersenne Twister.

@vks vks changed the title SeedableRng cannot be implemented for large RNGs SeedableRng cannot be implemented for large RNGs using the recommended seed type Mar 29, 2018
@vks
Copy link
Collaborator Author

vks commented Mar 29, 2018

I think either the documentation should discuss how to implement SeedableRng for large RNGs or the trait should be improved to support [u8; N] for N larger than 32.

@pitdicker pitdicker added the P-high Priority: high label Mar 29, 2018
@dhardy
Copy link
Member

dhardy commented Mar 29, 2018

8 × 32 = 256 bits which is more than enough for most purposes. Some key expansion algorithm should be used internally for larger state sizes I think. If you want compatibility with initialisation as used in another implementation, then I think a custom constructor is the best option.

@pitdicker
Copy link
Contributor

I really don't like that AsRef and AsMutare not available for common powers of 2. I am almost thinking if we should provide some array wrapper, like I added for ISAAC's result type in #325.

@pitdicker
Copy link
Contributor

But as you say, @dhardy, 256 bit's should just be enough.

@vks
Copy link
Collaborator Author

vks commented Mar 29, 2018

If we decide to do drop support for large RNGs, I think we should document the limitations of SeedableRng and suggest some workaround.

Using some key expansion is not really an option if reproducibility across implementations is needed. A custom constructor is not ideal, because it does not support from_rng.

@burdges
Copy link
Contributor

burdges commented Mar 29, 2018

We know const generics will solve this. If we need this sooner, then we should do a PR or RFC for the needed sizes as "preparation for const generics". It might encourage them to deliver const generics sooner. If it can wait till 2019 then just let it wait.

@vks
Copy link
Collaborator Author

vks commented Mar 30, 2018

Ok, I think the best workaround is to wrap the large seeds in a newtype and to implement then required traits by hand. No custom constructor required. It should be enough to update the documentation. I will submit a pull request.

@pitdicker
Copy link
Contributor

It should be enough to update the documentation. I will submit a pull request.
👍

Does anyone feel adventurous to make a PR to the rust repo to support some more common powers of two for AsRef and AsMut?

@burdges
Copy link
Contributor

burdges commented Mar 30, 2018

Right, new types are beneficial anyways, so maybe something like:

"We currently recommend that SeedableRng::Seed types be special purpose "newtypes" wrapped in a tuple struct, like pub struct XorShift1024Seed(pub [u8; 128]);. This practice encourages PRNG users to read any documentation relevant to seeding, leaves rand more flexibility to possibly support multiple seed types in the future, and supports PRNGs with large seed types."

@vks
Copy link
Collaborator Author

vks commented Mar 30, 2018 via email

@dhardy
Copy link
Member

dhardy commented Apr 4, 2018

@vks can I close this? Or do you want to make a documentation PR?

This is only a temporary problem anyway until const generics support is ready.

@vks
Copy link
Collaborator Author

vks commented Apr 4, 2018 via email

@dhardy
Copy link
Member

dhardy commented Apr 4, 2018

Good.

The doc on from_seed already suggests using replacement seeds instead if the input is not valid.

@vks
Copy link
Collaborator Author

vks commented Apr 9, 2018

@burdges I did not suggest to always use newtypes, because we are not doing it in Rand. If we decide to suggest this, I think we should also do it ourselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-high Priority: high
Projects
None yet
Development

No branches or pull requests

4 participants