MersenneTwisterRandomVariateGenerator: Simplify GetNextSeed(), and other style improvements#5318
Merged
dzenanz merged 5 commits intoInsightSoftwareConsortium:masterfrom Apr 22, 2025
Conversation
The pointer `r` is for read-access, whereas `s` is there to _set_ the state.
Replaced `MersenneTwisterRandomVariateGenerator::StateVectorLength` with just `StateVectorLength`, within the class implementation itself. Improved consistency, as `MersenneTwisterRandomVariateGenerator::PrintSelf` was already using the short notation.
Replaced a `GetSeed()` call with direct access to `m_Seed`, in GetNextSeed(). Following section "Accessing Members" of ITK's Coding Style Guide, https://github.com/InsightSoftwareConsortium/ITKSoftwareGuide/blob/v5.4.3/SoftwareGuide/Latex/Appendices/CodingStyleGuide.tex#L1708
This local variable inside the body of GetNextSeed() is no longer necessary.
Replaced clever `for (; i--; os << ...)` with simple range-based `for` loop.
dzenanz
approved these changes
Apr 22, 2025
N-Dekker
commented
Apr 22, 2025
Comment on lines
+174
to
181
| IntegerType * s = m_State; | ||
| const IntegerType * r = m_State; | ||
|
|
||
| *s++ = seed; | ||
| for (IntegerType i = 1; i < MersenneTwisterRandomVariateGenerator::StateVectorLength; ++i) | ||
| for (IntegerType i = 1; i < StateVectorLength; ++i) | ||
| { | ||
| *s++ = uint32_t{ (uint32_t{ 1812433253 } * (*r ^ (*r >> 30)) + i) }; | ||
| ++r; |
Contributor
Author
There was a problem hiding this comment.
Honestly it's not clear to me why it uses two pointers, s and r, instead of one. 🤷 Both pointers always just point to the same element of m_State.
Member
There was a problem hiding this comment.
Maybe kick out r pointer and leave only s?
Contributor
Author
There was a problem hiding this comment.
I just hesitated because the original C++ implementation appeared to have both an s and an r. At least, if this is the original implementation: https://www.cs.rpi.edu/academics/courses/fall12/ds/hw/05_multi_linked_lists/MersenneTwister.h
register uint32 *s = state;
register uint32 *r = state;
register int i = 1;
*s++ = seed & 0xffffffffUL;
for( ; i < N; ++i )
{
*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
r++;
}
But we did already make quite some style improvements compared to that one, anyway.
3aeba38
into
InsightSoftwareConsortium:master
15 of 16 checks passed
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.
Five little style improvements in the implementation of MersenneTwisterRandomVariateGenerator.