diff options
| author | Paschalis T <paschalis.tsilias@gmail.com> | 2024-08-16 01:29:18 +0300 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-08-19 20:47:27 +0000 |
| commit | d2b6bdb0350e9b2b8f704b214ce5936b72de043d (patch) | |
| tree | d6f86617b1c73f758580935458d0d213c251905f /src/math | |
| parent | d5f7da79870800ac2f7c2a68501f73a83d481d16 (diff) | |
| download | go-d2b6bdb0350e9b2b8f704b214ce5936b72de043d.tar.xz | |
math/rand: make calls to Seed no-op
Makes calls to the global Seed a no-op. The GODEBUG=randseednop=0
setting can be used to revert this behavior.
Fixes #67273
Change-Id: I79c1b2b23f3bc472fbd6190cb916a9d7583250f4
Reviewed-on: https://go-review.googlesource.com/c/go/+/606055
Auto-Submit: Cherry Mui <cherryyz@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/rand/rand.go | 11 | ||||
| -rw-r--r-- | src/math/rand/rand_test.go | 36 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/math/rand/rand.go b/src/math/rand/rand.go index 61ff5c1b38..4be1ca208a 100644 --- a/src/math/rand/rand.go +++ b/src/math/rand/rand.go @@ -313,6 +313,9 @@ var globalRandGenerator atomic.Pointer[Rand] var randautoseed = godebug.New("randautoseed") +// randseednop controls whether the global Seed is a no-op. +var randseednop = godebug.New("randseednop") + // globalRand returns the generator to use for the top-level convenience // functions. func globalRand() *Rand { @@ -391,7 +394,15 @@ func (fs *runtimeSource) read(p []byte, readVal *int64, readPos *int8) (n int, e // a random value. Programs that call Seed with a known value to get // a specific sequence of results should use New(NewSource(seed)) to // obtain a local random generator. +// +// As of Go 1.24 [Seed] is a no-op. To restore the previous behavior set +// GODEBUG=randseednop=0. func Seed(seed int64) { + if randseednop.Value() != "0" { + return + } + randseednop.IncNonDefault() + orig := globalRandGenerator.Load() // If we are already using a lockedSource, we can just re-seed it. diff --git a/src/math/rand/rand_test.go b/src/math/rand/rand_test.go index 7906f29674..1e1fad79ce 100644 --- a/src/math/rand/rand_test.go +++ b/src/math/rand/rand_test.go @@ -556,6 +556,42 @@ func TestUniformFactorial(t *testing.T) { } } +func TestSeedNop(t *testing.T) { + // If the global Seed takes effect, then resetting it to a certain value + // should provide predictable output to functions using it. + t.Run("randseednop=0", func(t *testing.T) { + t.Setenv("GODEBUG", "randseednop=0") + Seed(1) + before := Int63() + Seed(1) + after := Int63() + if before != after { + t.Fatal("global Seed should take effect") + } + }) + // If calls to the global Seed are no-op then functions using it should + // provide different output, even if it was reset to the same value. + t.Run("randseednop=1", func(t *testing.T) { + t.Setenv("GODEBUG", "randseednop=1") + Seed(1) + before := Int63() + Seed(1) + after := Int63() + if before == after { + t.Fatal("global Seed should be a no-op") + } + }) + t.Run("GODEBUG unset", func(t *testing.T) { + Seed(1) + before := Int63() + Seed(1) + after := Int63() + if before == after { + t.Fatal("global Seed should default to being a no-op") + } + }) +} + // Benchmarks func BenchmarkInt63Threadsafe(b *testing.B) { |
