Skip to content

Commit 6bb6b1e

Browse files
replace math/rand with math/rand/v2 (#5336)
* chore: use math/rand/v2 instead of math/rand * remove unnecessary conversion * remove unused changelog fragment --------- Co-authored-by: VihasMakwana <121151420+VihasMakwana@users.noreply.github.com>
1 parent dd397ae commit 6bb6b1e

File tree

8 files changed

+16
-17
lines changed

8 files changed

+16
-17
lines changed

internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package http
77
import (
88
"context"
99
"fmt"
10-
"math/rand"
10+
"math/rand/v2"
1111
"net/http"
1212
"net/http/httptest"
1313
"net/url"
@@ -111,8 +111,8 @@ func runTests(t *testing.T, testCases []testCase, config *artifact.Config, log *
111111
func getRandomTestCases() []testCase {
112112
tt := getTestCases()
113113

114-
first := rand.Intn(len(tt))
115-
second := rand.Intn(len(tt))
114+
first := rand.IntN(len(tt))
115+
second := rand.IntN(len(tt))
116116

117117
return []testCase{
118118
tt[first],

internal/pkg/agent/application/upgrade/marker_access_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11-
"math/rand"
11+
"math/rand/v2"
1212
"os"
1313
"path/filepath"
1414
"sync"
@@ -159,7 +159,7 @@ func randomBytes(length int) []byte {
159159

160160
var b []byte
161161
for i := 0; i < length; i++ {
162-
rune := chars[rand.Intn(len(chars))]
162+
rune := chars[rand.IntN(len(chars))]
163163
b = append(b, byte(rune))
164164
}
165165

internal/pkg/agent/cmd/enroll_cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12-
"math/rand"
12+
"math/rand/v2"
1313
"os"
1414
"os/exec"
1515
"strings"
@@ -704,7 +704,7 @@ func yamlToReader(in interface{}) (io.Reader, error) {
704704
}
705705

706706
func delay(ctx context.Context, d time.Duration) {
707-
t := time.NewTimer(time.Duration(rand.Int63n(int64(d))))
707+
t := time.NewTimer(rand.N(d))
708708
defer t.Stop()
709709
select {
710710
case <-ctx.Done():

internal/pkg/core/backoff/equal_jitter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package backoff
66

77
import (
8-
"math/rand"
8+
"math/rand/v2"
99
"time"
1010
)
1111

@@ -30,7 +30,7 @@ func NewEqualJitterBackoff(done <-chan struct{}, init, max time.Duration) Backof
3030
done: done,
3131
init: init,
3232
max: max,
33-
nextRand: time.Duration(rand.Int63n(int64(init))), //nolint:gosec
33+
nextRand: rand.N(init),
3434
}
3535
}
3636

@@ -51,7 +51,7 @@ func (b *EqualJitterBackoff) Wait() bool {
5151
backoff := b.NextWait()
5252

5353
// increase duration for next wait.
54-
b.nextRand = time.Duration(rand.Int63n(int64(b.duration)))
54+
b.nextRand = rand.N(b.duration)
5555
b.duration *= 2
5656
if b.duration > b.max {
5757
b.duration = b.max

internal/pkg/remote/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12-
"math/rand"
12+
"math/rand/v2"
1313
"net/http"
1414
"net/url"
1515
"sort"

internal/pkg/scheduler/scheduler.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package scheduler
66

77
import (
8-
"math/rand"
8+
"math/rand/v2"
99
"time"
1010
)
1111

@@ -129,6 +129,5 @@ func (p *PeriodicJitter) Stop() {
129129
}
130130

131131
func (p *PeriodicJitter) delay() time.Duration {
132-
t := int64(p.variance)
133-
return time.Duration(rand.Int63n(t))
132+
return rand.N(p.variance)
134133
}

magefile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"fmt"
1616
"html/template"
1717
"log"
18-
"math/rand"
18+
"math/rand/v2"
1919
"net/http"
2020
"os"
2121
"os/exec"

testing/integration/install_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package integration
99
import (
1010
"context"
1111
"fmt"
12-
"math/rand"
12+
"math/rand/v2"
1313
"os"
1414
"path/filepath"
1515
"runtime"
@@ -338,7 +338,7 @@ func randStr(length int) string {
338338

339339
runes := make([]rune, length)
340340
for i := range runes {
341-
runes[i] = letters[rand.Intn(len(letters))]
341+
runes[i] = letters[rand.IntN(len(letters))]
342342
}
343343

344344
return string(runes)

0 commit comments

Comments
 (0)