-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
std::rand: add MT19937 and MT19937-64. (Mersenne Twister RNG) #10029
Conversation
Implementations of the 32- and 64-bit variants of the Mersenne Twister RNG algorithm. C++11 mandates "mersenne_twister_engine" in its random number library, so this is just imitating that.
// This is ugly, but is it's necessary to be able to work directly | ||
// with SeedableRng: the seeding procedure for MT19937 differs for a | ||
// single integer and a vector. | ||
trait MT19937RngSeed { fn reseed(&self, &mut MT19937Rng); } |
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.
Shouldn't this be expressable via the SeedableRng
trait? I figured you'd have two implementations like:
impl SeedableRng<u32> for MT19937Rng { ... }
impl<'self> SeedableRng<&'self [u32]> for MT19937Rng { ... }
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.
Oh wait, I forgot we couldn't impl the same trait twice for the same type :(
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.
You can do it with an extra layer of indirection:
trait SeedInput { ... }
impl SeedInput for u32 { ... }
impl<'self> SeedInput for &'self [u32] { ... }
impl<I: SeedInput> SeedableRng<I> for MT19937Rng { ... }
It does require callers to use SeedInput
though :(
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.
@sfackler ... I don't understand how what you have there is different to the code as it stands.
(Also, I'm fairly sure you don't have to use SeedInput
.)
@alexcrichton yeah, it's unfortunate :(
This looks fantastic, nice job! Could you add some examples in the module documentation about how to use it. Just something which has the necessary imports and goes through the process of creating an Rng, reseeding it, and things like that. I don't think it really needs to demonstrate how to use The name of the module is pretty long: Regardless, with a doc-block expansion, r=me |
I could shorten it to |
|
How about |
I'm personally always in favor of shortening names when everything has a common prefix and it's pretty much implied by where it is, so I'm all for it! |
I'd like to question how many RNG's we need in the standard library. In general I don't believe we should providing an abundance of options for any particular need, but rather a single good default. Already we have a good CSPRNG and a fast RNG. What role does this one have? Is it fast enough to replace XorShift? Is it slow enough that Isaac is better for all purposes? |
@brson: Xorshift is ridiculously fast but the distribution can be very bad, and without good input parameters it can end up having a very short sequence of output before it repeats itself. I think there's a place for an RNG that's good enough for general non-cryptographic purposes, if it's actually significantly faster than ISAAC. If it's not, then we should just stick with ISAAC. |
I agree with @thestinger, fwiw. I'll get some numbers when I rebase on top of #9810, since that makes the benchmarks more representative. |
It would also be a good idea to look into replacing ISAAC with one of the eSTREAM ciphers, for both improved security (ISAAC has significant known weaknesses) and performance. Modern stream ciphers are designed for modern CPUs with vector processors. |
Citation? The only ones I know of is are Pudovkina and Aumasson (wikipedia agrees), and I don't think that either are considered significant. In any case, I was/am considering "upgrading" us to the ISAAC+ Aumasson proposes, but I believe that it has had even less cryptanalysis.
Filed #10047.
Hm, I don't remember it being slower.
I'll close this unless someone really thinks that mirroring C++11 is important. (I imagine that SFMT and/or dSFMT will be much faster, but they are more complicated to implement, so not today.) |
[`filter_next`]: suggest making binding mutable if it needs to be Fixes rust-lang#10029 changelog: [`filter_next`]: suggest making binding mutable if it needs to be and adjust applicability
Implementations of the 32- and 64-bit variants of the Mersenne Twister
RNG algorithm. C++11 mandates "mersenne_twister_engine" in its random
number library, so this is just imitating that.
(cc #9810, separate because that PR is large enough and this is a self-contained change.)