-
Notifications
You must be signed in to change notification settings - Fork 4.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
Batched random for the CSPRNG (and System.Random) #111211
Comments
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones |
Having public static void FillInt64(long toExclusive, Span<long> destination) {
for (int i = 0; i < destination.Length; i++) {
destination[i] = GetInt64(toExclusive);
}
} That's a squarer circle it seems. If we ever have a compelling reason to batch it, we can, if we can. |
It's not that Int64 can't be batched, it's just that isn't guaranteed to be. For batched UInt32, if the bound is For the homogeneous call, if If we also did a heterogeneous version of FillUInt64, it has the drawback that it might only get one value per batch (consider inputs of My proposal above does leave some corners cut, e.g.
Largely from "ok... assume none of these extras ever get called, how much work was wasted?". But I'm not fundamentally opposed to them. How much squaring would you like to see? |
Mostly just an observation. I think if we are going to add To someone not familiar with the batching and such, it seems odd that we added a bunch of 64-bit things to square but "forgot" the fill variants. |
Background and motivation
When generating many small random numbers, it is more efficient to generate them in one bulk operation. The most obvious example would be 8 coin-flips can be generated from just one random byte.
Several algorithms exist for extracting multiple bounded integers from a pull of an RNG, without sacrificing a loss of entropy. For example, if one needs two integers in the range [0, 6), they independently compose to a single integer in the range [0, 12). These algorithms can significantly speed up throughput on systems with a slow random number generator.
API Proposal
API Usage
Alternative Designs
int
returning instead ofvoid
, to indicate how many items they processed in a single batch.Risks
No response
The text was updated successfully, but these errors were encountered: