-
Notifications
You must be signed in to change notification settings - Fork 432
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
SmallRng
algorithm
#603
Comments
I should ping @pitdicker @vks @huonw @Lokathor @Ichoran who discussed this in the past and @huonw. dhardy#60 starts with a list of potential requirements for a general-purpose PRNG:
There is some question of the relative importance of each requirement (small PRNGs are all about trading off quality for speed and low memory usage), but the above is a good starting point. |
We can, if need be, select a different algorithm for Intel x86, x86_64, ARMv7, ARMv8 etc. It is not so easy to select a different algorithm for |
For now, I would just replace xorshift with PCG or xoshiro. In the long term, we might want to use a SIMD/AES-NI based RNG on platforms where that is possible. |
You can do a pcg32 as a single 64-bit number. Going smaller than that makes your quality drop off a cliff though. |
As @vks says, lets just select something reasonable for now. From this summary, lets use So, a job list, open to all takers:
Please make these crates sub-directories of the small-rngs repo. |
Consider that you have 32-bit machines in some places still, so you should probably pick a smaller gen on those machines, the 128 bit based generator is just too much overkill. |
Yes, the 128-bit generator appears to have terrible performance on x86, that's why I suggested we only use it on |
Ah, right. Forgot the naming for a moment. The only 32-bit machine I've used in ages is rpi boards and similar, which is all 32 bit ARM. |
There are decent generators with 128 bits of state that use 32 bit arithmetic (i.e. xoshiro). 64 bits of state is not enough for massively parallel applications. |
I'm fully familiar. PCG32 is in fact one such thing. |
The one suggested has 127 bits. It relies on PCG streams to achieve this so not ideal, but I think this is acceptable for now. |
You can also have a PCG32 variant that automatically jumps from stream to stream at some pre-determined point (such as seed = 0), effectively dumping all 127 bits of state into a single stream. Then again, if you have an important and massively parallel operation I don't think that you'll be using the least capable RNG that still functions that your platform offers. |
We don't need 127 bits of state in a single cycle. But for parallel usage, it's somewhat important that there is a low probability of two randomly-seeded instances are not close to one another, and a 64-bit seed is on the small side here. |
So, here's your options for Pcg32:
If none of those are what you want for SmallRNG, then you just don't want PCG32 as your SmallRNG. I'm sure that someone might pick to have as small a state as possible without regard to massively parallel operations. It might be best to worry less about picking particular RNGs for particular specially named RNG types, and just provide all the RNGs you can muster (that are reasonably effective) and then list some pros on each one and then have the |
@Lokathor this has all been discussed in various places. #431 has some plans for additional generators and we have a new repo to house these: small-rngs. @pitdicker evaluated the period and chance of overlap last November (see also requirements at the beginning of this issue: ~2^50 words cycle length, ~128 bits state size). Yes, we want the middle option you suggested. |
FYI: I've started on the |
@dhardy I didn't see your post here until now. I've also pushed up a rand_pcg crate which just contains the code @pitdicker wrote, while adjusting it to only depend on RngCore. https://github.com/coltfred/small-rngs/tree/pcg_algos |
Sorry, I actually started a couple of days ago but it took longer than I expected. Perhaps you could take a look at my PR? rust-random/small-rngs#4 |
This has now been implemented. |
This type is introduced as follows:
There is general consensus that the current algorithm, Xorshift, is not an ideal choice due to quality (poor results in PractRand and BigCrush) and trivial predictability (to the point that seeding one instance from another effectively creates a clone). Ideally we should replace this for the 0.6 release.
Existing work:
SmallRng
directlyweak_rng
andrandom
? #289 discusses replacingweak_rng
withSmallRng
I'm creating this new issue for visibility (most of the previous discussions happened on a fork) and to refresh this issue.
(First note: "reproducible" is used here to mean deterministic, portable, and a commitment not to tweak the algorithm in future updates. All reproducible PRNGs should eventually be migrated out of Rand proper to sub-libs, as discussed in dhardy#58 and #431.)
Question 1: is the current API with two non-reproducible PRNGs,
StdRng
andSmallRng
, the right one? As I understand the current choice is reasonably well accepted, although there have been suggestions to add more categories of PRNG.Question 2: which algorithm should we use for
SmallRng
? (The choice may vary by platform.)The text was updated successfully, but these errors were encountered: