Skip to content

MersenneTwisterRandomVariateGenerator: Simplify GetNextSeed(), and other style improvements#5318

Merged
dzenanz merged 5 commits intoInsightSoftwareConsortium:masterfrom
N-Dekker:MersenneTwisterRandomVariateGenerator-style-GetNextSeed
Apr 22, 2025
Merged

MersenneTwisterRandomVariateGenerator: Simplify GetNextSeed(), and other style improvements#5318
dzenanz merged 5 commits intoInsightSoftwareConsortium:masterfrom
N-Dekker:MersenneTwisterRandomVariateGenerator-style-GetNextSeed

Conversation

@N-Dekker
Copy link
Copy Markdown
Contributor

Five little style improvements in the implementation of MersenneTwisterRandomVariateGenerator.

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.
@github-actions github-actions bot added the area:Core Issues affecting the Core module label Apr 22, 2025
@N-Dekker N-Dekker marked this pull request as ready for review April 22, 2025 16:56
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;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe kick out r pointer and leave only s?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dzenanz dzenanz merged commit 3aeba38 into InsightSoftwareConsortium:master Apr 22, 2025
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants