You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposal is not concerned with the globalRand in the user-facing math/rand packages, rather the globalRand struct used to (AFAICT) seed the per-M rngs which in turn seed the runtimeSources the rand packages use as well as provide randomness for runtime decisions via cheaprand and its variants.
The use case for a user-specified seed it to provide a certain level of determinism and reproducibility when testing Go programs. Throughout Go's history, accidental determinism has been avoided as much as possible by randomizing select case selection, map iteration, and even goroutine scheduling (on the local run queue only AFAICT) when running with the race detector. However, purposeful determinism is a useful property to have when testing Go programs. A GORANDSEED environment variable would be a step towards letting users control and reproduce runtime randomness.
I recently wrote a blog post that achieves purposeful determinism (or close to it) for testing. Having a way to seed the runtime's randomness was a key ingredient.
Shortcomings
GORANDSEED by itself is not enough for purposeful determinism. One also needs to control the number of OS threads, among other things, so that random number generation order is stable across test runs. However, it is a key ingredient that the developer cannot currently control. Time, for example, can be controlled via -tags=faketime, and the number of OS threads by e.g. running the test on WASM.
Alternatives
An alternative to providing a seed via an environment variable is to intercept the os-specific readRandom implementations. One could, for example, use hermit to control /dev/urandom reads on unix systems. On WASM, some runtimes also seem to allow users to intercept random_get calls. However, these solutions are per-arch while something like GORANDSEED would work across architectures.
Proposal Details
Note
This proposal is not concerned with the
globalRandin the user-facingmath/randpackages, rather theglobalRandstruct used to (AFAICT) seed the per-M rngs which in turn seed theruntimeSources therandpackages use as well as provide randomness for runtime decisions viacheaprandand its variants.Proposal
Add a
GORANDSEEDenvironment variable or an equivalent option to seed runtime randomness. This environment variable will be observed inrandinitroughly like so: https://github.com/polarsignals/go/blob/ea083ca4892a62eb229c1886517e1cdb575ee19a/src/runtime/rand.go#L44-L51. This effectively replaces seeding via e.g. reading/dev/urandomon unix systems andrandom_getin WASM.Use case
The use case for a user-specified seed it to provide a certain level of determinism and reproducibility when testing Go programs. Throughout Go's history, accidental determinism has been avoided as much as possible by randomizing select case selection, map iteration, and even goroutine scheduling (on the local run queue only AFAICT) when running with the race detector. However, purposeful determinism is a useful property to have when testing Go programs. A
GORANDSEEDenvironment variable would be a step towards letting users control and reproduce runtime randomness.I recently wrote a blog post that achieves purposeful determinism (or close to it) for testing. Having a way to seed the runtime's randomness was a key ingredient.
Shortcomings
GORANDSEEDby itself is not enough for purposeful determinism. One also needs to control the number of OS threads, among other things, so that random number generation order is stable across test runs. However, it is a key ingredient that the developer cannot currently control. Time, for example, can be controlled via-tags=faketime, and the number of OS threads by e.g. running the test on WASM.Alternatives
An alternative to providing a seed via an environment variable is to intercept the os-specific
readRandomimplementations. One could, for example, use hermit to control/dev/urandomreads on unix systems. On WASM, some runtimes also seem to allow users to interceptrandom_getcalls. However, these solutions are per-arch while something likeGORANDSEEDwould work across architectures.