Skip to content

Commit db84c62

Browse files
committed
all: swap math/rand for math/rand/v2 where it doesn't affect behavior
math/rand is not and likely will never be deprecated, but math/rand/v2 is better in many ways, so switch to it. Apply two gopls suggested fixes along the way to use range-over-int. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ic4c1baff05bfef744fcd95a85d401ba982c75f89 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1216886 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 9f9ebac commit db84c62

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

internal/core/toposort/graph_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package toposort_test
1616

1717
import (
1818
"fmt"
19-
"math/rand"
19+
"math/rand/v2"
2020
"os"
2121
"slices"
2222
"strconv"
@@ -138,28 +138,28 @@ func TestSortFullyConnected(t *testing.T) {
138138
}
139139

140140
func TestSortRandom(t *testing.T) {
141-
seed := rand.Int63()
141+
seed := rand.Uint64()
142142
if str := os.Getenv("SEED"); str != "" {
143-
num, err := strconv.ParseInt(str, 10, 64)
143+
num, err := strconv.ParseUint(str, 10, 64)
144144
if err != nil {
145145
t.Fatalf("Could not parse SEED env var %q: %v", str, err)
146146
return
147147
}
148148
seed = num
149149
}
150150
t.Log("Seed", seed)
151-
rng := rand.New(rand.NewSource(seed))
151+
rng := rand.New(rand.NewPCG(seed, 123))
152152

153153
names := strings.Split("abcdefghijklm", "")
154154
index := runtime.New()
155155

156-
for n := 0; n < 100; n++ {
157-
inputs := make([][]string, 2+rng.Intn(4))
156+
for n := range 100 {
157+
inputs := make([][]string, 2+rng.IntN(4))
158158
for i := range inputs {
159159
names := slices.Clone(names)
160160
rng.Shuffle(len(names),
161161
func(i, j int) { names[i], names[j] = names[j], names[i] })
162-
inputs[i] = names[:2+rng.Intn(4)]
162+
inputs[i] = names[:2+rng.IntN(4)]
163163
}
164164

165165
t.Run(fmt.Sprint(n), func(t *testing.T) {

internal/mod/semver/semver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package semver
66

77
import (
8-
"math/rand"
8+
"math/rand/v2"
99
"slices"
1010
"strings"
1111
"testing"

internal/par/work.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package par
77

88
import (
99
"errors"
10-
"math/rand"
10+
"math/rand/v2"
1111
"sync"
1212
"sync/atomic"
1313
)
@@ -93,7 +93,7 @@ func (w *Work[T]) runner() {
9393
// to eliminate pathological contention
9494
// in case items added at about the same time
9595
// are most likely to contend.
96-
i := rand.Intn(len(w.todo))
96+
i := rand.IntN(len(w.todo))
9797
item := w.todo[i]
9898
w.todo[i] = w.todo[len(w.todo)-1]
9999
w.todo = w.todo[:len(w.todo)-1]

mod/modcache/fetch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"io"
88
"io/fs"
99
"log"
10-
"math/rand"
10+
"math/rand/v2"
1111
"os"
1212
"path/filepath"
1313
"slices"
@@ -361,8 +361,8 @@ func quoteGlob(s string) string {
361361

362362
// tempFile creates a new temporary file with given permission bits.
363363
func tempFile(ctx context.Context, dir, prefix string, perm fs.FileMode) (f *os.File, err error) {
364-
for i := 0; i < 10000; i++ {
365-
name := filepath.Join(dir, prefix+strconv.Itoa(rand.Intn(1000000000))+".tmp")
364+
for range 10000 {
365+
name := filepath.Join(dir, prefix+strconv.Itoa(rand.IntN(1000000000))+".tmp")
366366
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)
367367
if os.IsExist(err) {
368368
if ctx.Err() != nil {

0 commit comments

Comments
 (0)