Skip to content

Conversation

@FranciscoThiesen
Copy link

Summary

  • Implement batched random number generation for shuffle based on Brackett-Rozinsky & Lemire's paper
  • For URNGs with full 64-bit range (like mt19937_64), generate 2 bounded random integers from a single 64-bit random value
  • Uses Lemire's nearly-divisionless algorithm for rejection sampling

Implementation Details

  • Added _Batched_rng_from_urng struct that extracts batches of bounded random integers
  • Added _Random_shuffle_batched function implementing Fisher-Yates with batched RNG
  • Modified shuffle() and ranges::shuffle to use batched version for eligible URNGs
  • Compile-time dispatch based on URNG characteristics (full 64-bit range)

Test Plan

  • Existing shuffle tests pass
  • Added permutation correctness tests for both 64-bit (batched) and 32-bit (non-batched) paths
  • Added edge case tests (empty, single element, 2, 3, 4, and 10000 elements)

Resolves #5736

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
@FranciscoThiesen
Copy link
Author

@FranciscoThiesen please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree [company="Microsoft"]

@FranciscoThiesen
Copy link
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.
@StephanTLavavej StephanTLavavej added the performance Must go faster label Dec 4, 2025
@StephanTLavavej StephanTLavavej changed the title Implement batched random integer generation for shuffle <algorithm>: Implement batched random integer generation for shuffle() Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Must go faster

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

<algorithm>: Investigate further optimizations for shuffle() and sample()

3 participants