<algorithm>: Implement batched random integer generation for shuffle()#5932
Merged
StephanTLavavej merged 18 commits intomicrosoft:mainfrom Feb 24, 2026
Conversation
Implements the algorithm from Brackett-Rozinsky & Lemire's paper "Batched Ranged Random Integer Generation" to reduce RNG calls in std::shuffle and ranges::shuffle for 64-bit URNGs like mt19937_64. The batched approach extracts multiple bounded random integers from a single 64-bit random word, using only multiplication (no division) in the common case. This reduces the number of RNG calls by approximately half for arrays with fewer than 2^32 elements. Resolves microsoft#5736
Contributor
Author
@microsoft-github-policy-service agree [company="Microsoft"] |
Contributor
Author
|
@microsoft-github-policy-service agree company="Microsoft" |
Move _Has_full_64bit_range check out of _Batched_rng_from_urng class to reduce template instantiation overhead. Use is_same_v and _Max_limit as suggested by reviewer.
<algorithm>: Implement batched random integer generation for shuffle()
…tatic assertion.
StephanTLavavej
approved these changes
Feb 21, 2026
Member
|
I'm mirroring this to the MSVC-internal repo. Please notify me if any further changes are pushed, otherwise no action is required. |
Member
🚀 🎲 😻 |
|
@StephanTLavavej My employer had be take a training course where we were told that emojis are unprofessional and should be avoided. 🔥 🚀 Go! |
This was referenced Feb 25, 2026
FranciscoThiesen
added a commit
to FranciscoThiesen/STL
that referenced
this pull request
Feb 27, 2026
Fix two bugs in the batched shuffle implementation from PR microsoft#5932: 1. `_Unsigned128` brace-initialization zeroed the high word of the multiplication result (the random index), making all indices 0 and shuffle deterministic. Replace with separate `uint64_t` variables for high and low words in both `_Single_bounded` and `_Batch_2`. 2. Loop advancement in `_Random_shuffle_batched` and `_Shuffle_unchecked_batched` had extra `++_UTarget` increments causing elements to be skipped. Restructure to `for` loops matching the original `_Random_shuffle1` pattern. Add regression test for microsoftGH-6112 and shuffle quality tests adapted from Lemire's cpp_batched_random test suite (uniformity, coverage, pair distribution at start/end positions). Co-Authored-By: Claude <noreply@anthropic.com>
FranciscoThiesen
added a commit
to FranciscoThiesen/STL
that referenced
this pull request
Feb 28, 2026
Fix two bugs in the batched shuffle implementation from PR microsoft#5932: 1. `_Unsigned128` brace-initialization zeroed the high word of the multiplication result (the random index), making all indices 0 and shuffle deterministic. Replace with separate `uint64_t` variables for high and low words in both `_Single_bounded` and `_Batch_2`. 2. Loop advancement in `_Random_shuffle_batched` and `_Shuffle_unchecked_batched` had extra `++_UTarget` increments causing elements to be skipped. Restructure to `for` loops matching the original `_Random_shuffle1` pattern. Add regression test for microsoftGH-6112 and shuffle quality tests adapted from Lemire's cpp_batched_random test suite (uniformity, coverage, pair distribution at start/end positions). Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implementation Details
Test Plan
Resolves #5736