Repository navigation
fix(random): MT19937.SetState refuses a position outside [0, 624] - #134
Merged
Merged
Conversation
SetState stored Pos as given. Uint32 regenerates the key only when pos == 624, so a negative Pos or one above 624 indexed the 624-word key out of range (and genNext/addState, reached through Jumped, as well). Reported by go-embedded-r while binding R's RNG. SetState now returns an error for such a Pos and leaves the generator unchanged; RandomState.SetState passes it on. Calls that ignored the (previously absent) result still compile. Tests: both bounds refused (-1, -624, 625, 2^40) with the state kept and a draw still safe; Pos 0 and 624 accepted and continuing the stream; FuzzMT19937SetState over Pos and key words (20 s locally, no failure). With the guard disabled the refusal test fails and Uint32 panics with index out of range [-1]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 << 40 overflows int on 386 and arm, so the test file did not compile on the 32-bit lanes. Use math.MaxInt and math.MinInt, which also cover both extremes on every word size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tannevaled
force-pushed
the
fix/mt19937-setstate-pos
branch
from
November 29, 2026 07:16
2fef3a5 to
c1cc030
Compare
…tState errors From the review of #134: - the refusal test now also flips HasGauss/Gauss in the refused state and asserts that GetState after the refusal equals the state before it (NumPy writes gauss before the bit-generator setter refuses; we must not); - api_test.go checks the error that MT19937.SetState now returns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <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.
What
MT19937.SetState(random/mt19937.go) storedPosas given.Uint32regenerates the key only whenpos == 624, so a negativePos, or one above 624, indexed the 624-word key out of range.genNextandaddState, whichJumpedreaches, did the same. go-embedded-r found this while binding R's RNG.MT19937.SetStatenow returns anerrorfor aPosoutside [0, 624] and leaves the generator unchanged.RandomState.SetStatepasses that error on.API:
SetStategains anerrorresult. Calls that use it as a statement still compile. No interface in the package includesSetState.Tests
TestMT19937SetStateRefusesPosOutOfRange: Pos -1, -624, 625 and 2^40 are each refused. The state is kept and a draw still works. The same holds throughRandomState.TestMT19937SetStateAcceptsBothEnds: Pos 0 and Pos 624 are accepted and continue the stream draw for draw. The test asserts that both ends were actually reached.FuzzMT19937SetState: over Pos and the key words, SetState either refuses, or leaves a generator that can draw, jump and draw again. It ran for 20 s locally with no failure; the seeds are inf.Add.mutsweepon the two changed refusals:mt19937.go:166is caught by an assertion. With the guard disabled,Uint32panics withindex out of range [-1], which is the reported bug.legacy.go:109is caught by an assertion.Performance
SetStateruns once per state transfer, outside any draw loop (docs/perf-exempt.md:49).Uint32is unchanged.Checks run locally:
gofmt -lprints nothing andgo test ./random/is green.🤖 Generated with Claude Code