-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement proposal #54880, to automatically seed the global source. The justification for this not being a breaking change is that any use of the global source in a package's init function or exported API clearly must be valid - that is, if a package changes how much randomness it consumes at init time or in an exported API, that clearly isn't the kind of breaking change that requires issuing a v2 of that package. That kind of per-package change in the position of the global source is indistinguishable from seeding the global source differently. So if the per-package change is valid, so is auto-seeding. And then, of course, auto-seeding means that packages will be far less likely to depend on the specific results of the global source and therefore not break when those kinds of per-package changes happen in the future. Seed(1) can be called in programs that need the old sequence from the global source and want to restore the old behavior. Of course, those programs will still be broken by the per-package changes just described, and it would be better for them to allocate local sources rather than continue to use the global one. Fixes #54880. Change-Id: Ib9dc3307b97f7a45587a9cc50d81f919d3edc7ae Reviewed-on: https://go-review.googlesource.com/c/go/+/443058 Reviewed-by: Austin Clements <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Russ Cox <[email protected]>
- Loading branch information
Showing
3 changed files
with
81 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package rand_test | ||
|
||
import ( | ||
. "math/rand" | ||
"testing" | ||
) | ||
|
||
// This test is first, in its own file with an alphabetically early name, | ||
// to try to make sure that it runs early. It has the best chance of | ||
// detecting deterministic seeding if it's the first test that runs. | ||
|
||
func TestAuto(t *testing.T) { | ||
// Pull out 10 int64s from the global source | ||
// and then check that they don't appear in that | ||
// order in the deterministic Seed(1) result. | ||
var out []int64 | ||
for i := 0; i < 10; i++ { | ||
out = append(out, Int63()) | ||
} | ||
|
||
// Look for out in Seed(1)'s output. | ||
// Strictly speaking, we should look for them in order, | ||
// but this is good enough and not significantly more | ||
// likely to have a false positive. | ||
Seed(1) | ||
found := 0 | ||
for i := 0; i < 1000; i++ { | ||
x := Int63() | ||
if x == out[found] { | ||
found++ | ||
if found == len(out) { | ||
t.Fatalf("found unseeded output in Seed(1) output") | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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