Skip to content

feat: allow reuse of random source#37

Open
gammazero wants to merge 5 commits into
mainfrom
reuse-rand-source
Open

feat: allow reuse of random source#37
gammazero wants to merge 5 commits into
mainfrom
reuse-rand-source

Conversation

@gammazero

@gammazero gammazero commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Changes to go-test module.

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.

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.

Finally, this PR migrates from /math/rand/ to /math/rand/v2 and uses ChaCha8 as its random number source.

Summary of changes in this PR:

  • Enable reuse of pseudo-random source for data generation
  • Internal efficiency improvements due to 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.
  • Move from /math/rand to /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

  • 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
  • Deprecated NewRand and NewSeededRand
  • Make Sequence a separate type
  • Added NewSeed function to create new seed of type [32]byte
  • Added MakeSeed function to create [32]byte seed from uint64

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.
gammazero added 4 commits July 9, 2026 05:15
- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants