Skip to content

Commit f5003e0

Browse files
authored
Migrate from math/rand to math/rand/v2 (#4952)
## Summary - Replace `math/rand` with `math/rand/v2` and `Intn` with `IntN` in the two files that used it - Add depguard lint rule to prevent reintroduction of `math/rand` v1 Per the [Go 1.22 release notes](https://go.dev/doc/go1.22#math/rand/v2), `math/rand/v2` is the recommended replacement for `math/rand`. This pull request was AI-assisted by Isaac.
1 parent 0ef180d commit f5003e0

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

.golangci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ linters:
3434
deny:
3535
- pkg: "github.com/databricks/cli/experimental"
3636
desc: "must not import experimental/ packages; use an interface or move the dependency"
37+
no-legacy-rand:
38+
deny:
39+
- pkg: "math/rand$"
40+
desc: "use math/rand/v2 instead of math/rand"
3741
forbidigo:
3842
forbid:
3943
- pattern: 'term\.IsTerminal'

integration/libs/locker/locker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66
"io"
77
"io/fs"
8-
"math/rand"
8+
"math/rand/v2"
99
"sync"
1010
"testing"
1111
"time"
@@ -40,7 +40,7 @@ func TestLock(t *testing.T) {
4040
var wg sync.WaitGroup
4141
for currentIndex := range numConcurrentLocks {
4242
wg.Go(func() {
43-
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
43+
time.Sleep(time.Duration(rand.IntN(100)) * time.Millisecond)
4444
lockerErrs[currentIndex] = lockers[currentIndex].Lock(ctx, false)
4545
})
4646
}

libs/template/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"math/rand"
7+
"math/rand/v2"
88
"net/url"
99
"os"
1010
"regexp"
@@ -66,9 +66,9 @@ func loadHelpers(ctx context.Context) template.FuncMap {
6666
"regexp": func(expr string) (*regexp.Regexp, error) {
6767
return regexp.Compile(expr)
6868
},
69-
// Alias for https://pkg.go.dev/math/rand#Intn. Returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n).
69+
// Alias for https://pkg.go.dev/math/rand/v2#IntN. Returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n).
7070
"random_int": func(n int) int {
71-
return rand.Intn(n)
71+
return rand.IntN(n)
7272
},
7373
// Alias for https://pkg.go.dev/github.com/google/uuid#New. Returns, as a string, a UUID which is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.
7474
"uuid": func() string {

0 commit comments

Comments
 (0)