diff --git a/pkg/com/net_test.go b/pkg/com/net_test.go index 572fb31e..364c5c25 100644 --- a/pkg/com/net_test.go +++ b/pkg/com/net_test.go @@ -3,7 +3,7 @@ package com import ( "encoding/json" "fmt" - "math/rand" + "math/rand/v2" "net" "net/http" "net/url" @@ -90,12 +90,11 @@ func testWebsocket(t *testing.T) { for _, call := range calls { call := call if call.concurrent { - rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < n; i++ { packet := call.packet go func() { defer wait.Done() - time.Sleep(time.Duration(rand.Intn(200-100)+100) * time.Millisecond) + time.Sleep(time.Duration(rand.IntN(200-100)+100) * time.Millisecond) vv, err := client.rpc.Call(client.sock.conn, &packet) err = checkCall(vv, err, call.value) if err != nil { diff --git a/pkg/encoder/yuv/yuv_test.go b/pkg/encoder/yuv/yuv_test.go index 2575d486..75a61bc2 100644 --- a/pkg/encoder/yuv/yuv_test.go +++ b/pkg/encoder/yuv/yuv_test.go @@ -8,7 +8,7 @@ import ( "image/png" "io" "math" - "math/rand" + "math/rand/v2" "os" "path/filepath" "testing" @@ -169,7 +169,7 @@ func BenchmarkYuv(b *testing.B) { {w: 1920, h: 1080}, {w: 320, h: 240}, } - r1 := rand.New(rand.NewSource(int64(1))).Float32() + r1 := rand.Float32() for _, test := range tests { w, h := test.w, test.h diff --git a/pkg/games/launcher.go b/pkg/games/launcher.go index 78bcdae3..ad20ab0f 100644 --- a/pkg/games/launcher.go +++ b/pkg/games/launcher.go @@ -2,7 +2,7 @@ package games import ( "fmt" - "math/rand" + "math/rand/v2" "strconv" "strings" ) @@ -59,5 +59,5 @@ func ExtractGame(roomID string) string { // RoomID contains random number + gameName // Next time when we only get roomID, we can launch game based on gameName func GenerateRoomID(title string) string { - return strconv.FormatInt(rand.Int63(), 16) + separator + title + return strconv.FormatInt(rand.Int64(), 16) + separator + title } diff --git a/pkg/worker/caged/libretro/frontend_test.go b/pkg/worker/caged/libretro/frontend_test.go index e10d289e..81709e22 100644 --- a/pkg/worker/caged/libretro/frontend_test.go +++ b/pkg/worker/caged/libretro/frontend_test.go @@ -5,7 +5,7 @@ import ( "fmt" "io" "log" - "math/rand" + "math/rand/v2" "os" "path/filepath" "sync" @@ -333,7 +333,7 @@ func TestConcurrentInput(t *testing.T) { wg.Add(2 * events) for i := 0; i < events; i++ { - player := rand.Intn(maxPort) + player := rand.IntN(maxPort) go func() { state.setInput(player, []byte{0, 1}); wg.Done() }() go func() { state.isKeyPressed(uint(player), 100); wg.Done() }() } @@ -362,4 +362,4 @@ func expand(p ...string) string { func hash(bytes []byte) string { return fmt.Sprintf("%x", md5.Sum(bytes)) } // lucky returns random boolean. -func lucky() bool { return rand.Intn(2) == 1 } +func lucky() bool { return rand.IntN(2) == 1 } diff --git a/pkg/worker/media/media_test.go b/pkg/worker/media/media_test.go index 8db75e6c..a14fd02a 100644 --- a/pkg/worker/media/media_test.go +++ b/pkg/worker/media/media_test.go @@ -2,7 +2,7 @@ package media import ( "image" - "math/rand" + "math/rand/v2" "reflect" "testing" @@ -25,8 +25,8 @@ func TestEncoders(t *testing.T) { } for _, test := range tests { - a := genTestImage(test.w, test.h, rand.New(rand.NewSource(int64(1))).Float32()) - b := genTestImage(test.w, test.h, rand.New(rand.NewSource(int64(2))).Float32()) + a := genTestImage(test.w, test.h, rand.Float32()) + b := genTestImage(test.w, test.h, rand.Float32()) for i := 0; i < test.n; i++ { run(test.w, test.h, test.codec, test.frames, a, b, t) } @@ -71,10 +71,10 @@ func run(w, h int, cod encoder.VideoCodec, count int, a *image.RGBA, b *image.RG defer ve.Stop() if a == nil { - a = genTestImage(w, h, rand.New(rand.NewSource(int64(1))).Float32()) + a = genTestImage(w, h, rand.Float32()) } if b == nil { - b = genTestImage(w, h, rand.New(rand.NewSource(int64(2))).Float32()) + b = genTestImage(w, h, rand.Float32()) } for i := 0; i < count; i++ { @@ -146,7 +146,7 @@ func BenchmarkResampler(b *testing.B) { func gen(l int) []int16 { nums := make([]int16, l) for i := range nums { - nums[i] = int16(rand.Intn(10)) + nums[i] = int16(rand.IntN(10)) } return nums } diff --git a/pkg/worker/recorder/recorder.go b/pkg/worker/recorder/recorder.go index d0c1165b..5c1bf904 100644 --- a/pkg/worker/recorder/recorder.go +++ b/pkg/worker/recorder/recorder.go @@ -2,7 +2,7 @@ package recorder import ( "io" - "math/rand" + "math/rand/v2" "os" "path/filepath" "regexp" @@ -204,7 +204,7 @@ func random(num string) string { } b := make([]byte, n) for i := range b { - b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))] + b[i] = letterBytes[rand.Int64()%int64(len(letterBytes))] } return string(b) } diff --git a/pkg/worker/recorder/recorder_test.go b/pkg/worker/recorder/recorder_test.go index 8d2fa998..fcfe1f57 100644 --- a/pkg/worker/recorder/recorder_test.go +++ b/pkg/worker/recorder/recorder_test.go @@ -5,7 +5,7 @@ import ( "image" "image/color" "log" - "math/rand" + "math/rand/v2" "os" "sync" "sync/atomic" @@ -147,13 +147,11 @@ func genFrame(w, h int) Frame { } } -var rnd = rand.New(rand.NewSource(time.Now().Unix())) - func randomColor() color.RGBA { return color.RGBA{ - R: uint8(rnd.Intn(256)), - G: uint8(rnd.Intn(256)), - B: uint8(rnd.Intn(256)), + R: uint8(rand.IntN(256)), + G: uint8(rand.IntN(256)), + B: uint8(rand.IntN(256)), A: 255, } }