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

rand::rngs::StdRng produces different values in/out struct #1463

Closed
TOPbuaa opened this issue Jun 26, 2024 · 7 comments
Closed

rand::rngs::StdRng produces different values in/out struct #1463

TOPbuaa opened this issue Jun 26, 2024 · 7 comments
Labels
X-bug Type: bug report

Comments

@TOPbuaa
Copy link

TOPbuaa commented Jun 26, 2024

Code sample

I provide a minimal repo with 40 lines of code for this issue: https://github.com/TOPbuaa/mini_test_rand.

Summary

When I use rand inside a struct with the same seed, the results are different from when it is outside the struct.
Is my usage wrong? or is there a bug causing undefined behavior?

@TOPbuaa TOPbuaa added the X-bug Type: bug report label Jun 26, 2024
@dhardy
Copy link
Member

dhardy commented Jun 26, 2024

This can be run from the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8d72ab06f71630e19818510ea55b2261

I noticed that commenting out one line in particular affects the result.

@dhardy
Copy link
Member

dhardy commented Jun 26, 2024

Testing with v0.9.0-alpha.1 yields the same sequence for direct usage of StdRng and a different sequence for usage via the struct. Testing with the latest (ca9e119) yields the same results as v0.9.0-alpha.1.

@dhardy
Copy link
Member

dhardy commented Jun 26, 2024

Without diving into the code, it is feasible that there is a bug in rand_chacha or one of its dependencies. These use unsafe CPU-specific optimisations, which seems the most likely cause.

We have plans to replace this entire implementation (#934), so this bug might get fixed as a side-effect.

@TheIronBorn
Copy link
Contributor

I don't think it's an issue with ChaCha. I just reproduced this issue with the Isaac rngs, Hc128, and various Xorshift/Xoshiro/Xoroshiro rngs

@TheIronBorn
Copy link
Contributor

Ah it's because the compiler is changing the type of gen_range. gen_range(0_u64 fixes it

@TOPbuaa
Copy link
Author

TOPbuaa commented Jun 27, 2024

Ah it's because the compiler is changing the type of gen_range. gen_range(0_u64 fixes it

Fine. Adding the _u64 suffix works well for me.

@dhardy dhardy closed this as completed Jun 27, 2024
@vks
Copy link
Collaborator

vks commented Jun 27, 2024

For clarity: In the struct, you are specifying the type as u64, which gets propagated to gen_range. Outside the struct, you are not specifying the type, and therefore the compiler falls back to the default integer type, which is i32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
X-bug Type: bug report
Projects
None yet
Development

No branches or pull requests

4 participants