feat: allow reuse of random source#37
Open
gammazero wants to merge 5 commits into
Open
Conversation
For tests that need to keep generating random test data, it is more efficient to continue using the same random source instead of creating a new one each time data is generated. This also applies to the generation logic itself when one part of the generation logic uses another. This PR lets the caller create a pseudo-random number source that can be uses reused for generating test data. This is also use within the test logic itself to avoid creating unnecessary random sources. Also included in this PR are two new functions, `Name` and `NameSize`, that generate fixed-size and random-size random name strings. Finally this PR fixes two minor defects in the `random/files` package. First, random-size names never reached the maximum size. Second, the file names were more likely to contain a "0" due to its inclusion twice in the alphabet used for file name generation. In summary, this PR: - Enables reuse of pseudo-random source for data generation - Makes some data generation logic more efficient by reuse of random source - Fixes minor defects with random file name generation - Increases test coverage. - Adds two new APIs `Name` and `NameSize`, for generating fixes and random-sized random names.
guillaumemichel
approved these changes
Jul 9, 2026
- Upgrade to math/rand/v2 - Seed value changed from uint64 to [32]byte as needed for ChaCha8 - Added Name and NameSize functions to API - Do not support global seed - Do not support global sequence - Remove NewRand and NewSeededRand - Make sequence a separate type - Added NewSeed function to create new seed. - Added MakeSeed function to create seed from uint64 The move to a different number generator, ChaCha8, means that different values will be generated than before, and tests depending on deterministic random values need to be updated.
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.
Changes to
go-testmodule.For tests that need to keep generating random test data, it is more efficient to continue using the same random source instead of creating a new one each time data is generated. This also applies to the generation logic itself when one part of the generation logic uses another.
This PR lets the caller create a pseudo-random number source that can be uses reused for generating test data. This is also use within the test logic itself to avoid creating unnecessary random sources.
Also included in this PR are two new functions,
NameandNameSize, that generate fixed-size and random-size random name strings.This PR fixes two minor defects in the
random/filespackage. First, random-size names never reached the maximum size. Second, the file names were more likely to contain a "0" due to its inclusion twice in the alphabet used for file name generation.Finally, this PR migrates from
/math/rand/to/math/rand/v2and uses ChaCha8 as its random number source.Summary of changes in this PR:
NameandNameSize, for generating fixes and random-sized random names./math/randto /math/rand/v2`The move to a different number generator, ChaCha8, means that different values will be generated than before, and tests depending on deterministic random values need to be updated.
API Changes
NewRandandNewSeededRandSequencea separate typeNewSeedfunction to create new seed of type[32]byteMakeSeedfunction to create[32]byteseed fromuint64