diff --git a/src/sort/sort_slices_benchmark_test.go b/src/sort/sort_slices_benchmark_test.go index 37f3b1bc7e4..069536df03b 100644 --- a/src/sort/sort_slices_benchmark_test.go +++ b/src/sort/sort_slices_benchmark_test.go @@ -5,7 +5,7 @@ package sort_test import ( - "math/rand" + "math/rand/v2" "slices" . "sort" "strconv" @@ -18,10 +18,10 @@ import ( // package). func makeRandomInts(n int) []int { - rand.Seed(42) + r := rand.New(rand.NewPCG(42, 0)) ints := make([]int, n) for i := 0; i < n; i++ { - ints[i] = rand.Intn(n) + ints[i] = r.IntN(n) } return ints } @@ -92,14 +92,14 @@ func BenchmarkSlicesIsSorted(b *testing.B) { // makeRandomStrings generates n random strings with alphabetic runes of // varying lengths. func makeRandomStrings(n int) []string { - rand.Seed(42) + r := rand.New(rand.NewPCG(42, 0)) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") ss := make([]string, n) for i := 0; i < n; i++ { var sb stringspkg.Builder - slen := 2 + rand.Intn(50) + slen := 2 + r.IntN(50) for j := 0; j < slen; j++ { - sb.WriteRune(letters[rand.Intn(len(letters))]) + sb.WriteRune(letters[r.IntN(len(letters))]) } ss[i] = sb.String() } @@ -156,10 +156,10 @@ func (s myStructs) Less(i, j int) bool { return s[i].n < s[j].n } func (s myStructs) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func makeRandomStructs(n int) myStructs { - rand.Seed(42) + r := rand.New(rand.NewPCG(42, 0)) structs := make([]*myStruct, n) for i := 0; i < n; i++ { - structs[i] = &myStruct{n: rand.Intn(n)} + structs[i] = &myStruct{n: r.IntN(n)} } return structs } diff --git a/src/sort/sort_test.go b/src/sort/sort_test.go index ccb89873afb..ba757a845aa 100644 --- a/src/sort/sort_test.go +++ b/src/sort/sort_test.go @@ -9,7 +9,7 @@ import ( "fmt" "internal/testenv" "math" - "math/rand" + "math/rand/v2" "slices" . "sort" "strconv" @@ -110,7 +110,7 @@ func TestSortLarge_Random(t *testing.T) { } data := make([]int, n) for i := 0; i < len(data); i++ { - data[i] = rand.Intn(100) + data[i] = rand.IntN(100) } if IntsAreSorted(data) { t.Fatalf("terrible rand.rand") @@ -198,7 +198,7 @@ func TestNonDeterministicComparison(t *testing.T) { }() td := &nonDeterministicTestingData{ - r: rand.New(rand.NewSource(0)), + r: rand.New(rand.NewPCG(0, 0)), } for i := 0; i < 10; i++ { @@ -442,13 +442,13 @@ func testBentleyMcIlroy(t *testing.T, sort func(Interface), maxswap func(int) in case _Sawtooth: data[i] = i % m case _Rand: - data[i] = rand.Intn(m) + data[i] = rand.IntN(m) case _Stagger: data[i] = (i*m + i) % n case _Plateau: data[i] = min(i, m) case _Shuffle: - if rand.Intn(m) != 0 { + if rand.IntN(m) != 0 { j += 2 data[i] = j } else { @@ -648,7 +648,7 @@ func TestStability(t *testing.T) { // random distribution for i := 0; i < len(data); i++ { - data[i].a = rand.Intn(m) + data[i].a = rand.IntN(m) } if IsSorted(data) { t.Fatalf("terrible rand.rand") @@ -704,7 +704,7 @@ func countOps(t *testing.T, algo func(Interface), name string) { maxswap: 1<<31 - 1, } for i := 0; i < n; i++ { - td.data[i] = rand.Intn(n / 5) + td.data[i] = rand.IntN(n / 5) } algo(&td) t.Logf("%s %8d elements: %11d Swap, %10d Less", name, n, td.nswap, td.ncmp)