feat: Add SetRandomSeed and ResetRandomSeed for reproducible random sequences#763
Open
ukpratik wants to merge 4 commits intosamber:masterfrom
Open
feat: Add SetRandomSeed and ResetRandomSeed for reproducible random sequences#763ukpratik wants to merge 4 commits intosamber:masterfrom
ukpratik wants to merge 4 commits intosamber:masterfrom
Conversation
Author
| @samber please check my new implementation for adding seed in xrand for test, I have detailed information for the same in the description, let me know what do you think about this. |
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.
Summary
This PR adds the ability to seed the random number generator used by
lofunctions, enabling reproducible random sequences. This is particularly useful for testing scenarios where deterministic behavior is required.Motivation
When writing tests that involve random operations (e.g.,
Shuffle,RandomString,), it's often necessary to have predictable, reproducible results. Without the ability to seed the RNG, tests can be flaky or require workarounds.Let me know if you think any changes that I should do in this PR or ay guidance, feel free to push any minor changes if required.
Use case example:
Changes
New Public API
SetRandomSeed(seed int64)ResetRandomSeed()SetRandomSeed(-1).Files Changed
rand.go- New public API functions with documentationrand_test.go- Comprehensive unit testsinternal/xrand/ordered_go118.go- Implementation for Go < 1.22internal/xrand/ordered_go122.go- Implementation for Go ≥ 1.22benchmark/slice_benchmark_test.go- AddedBenchmarkShuffleTesting
✅ Unit tests added covering:
RandomStringwith same seedShufflewith same seedBenchmarks
No performance regression detected.
Benchmark comparison (10 runs each):
Memory allocations: 0 B/op (unchanged)
Full benchmark output
Benchmark report for New changes:
Benchmark report for Existing Code: