Repository navigation
Warning about discard() by powers of two with power-of-two moduli - #173
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #173 +/- ##
========================================
Coverage 95.80% 95.80%
========================================
Files 118 118
Lines 7653 7653
========================================
Hits 7332 7332
Misses 321 321 Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
mborland
approved these changes
Nov 3, 2026
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.
This PR adds a warning to the documentation of the generators: for linear congruential generators with a power-of-two modulus, such as
rand48,discard()by a power of two yields strongly correlated streams. Sincediscard()is logarithmic, it is natural to use it to split the period into subsequences, and 2^47 (half the period ofrand48) is a natural choice. However, these are the first outputs of a default-constructedrand48and of a copy afterdiscard(UINT64_C(1) << 47):The two streams differ only in the top bit, at every step. This happens because, modulo 2^48, bit j of the state has period 2^(j+1), so a jump by 2^e leaves the lowest e bits of the state unchanged; as
rand48returns bits 17 to 47 of the state,discard()by 2^(17+t) leaves the lowest t bits of the outputs of the two streams identical. In the case of 2^47, the two states differ by 2^47 at every step, that is, only in their top bit.The other linear congruential generators of the library (
minstd_rand0,minstd_rand, and those ofecuyer1988) are multiplicative generators with a prime modulus, and are not affected.