From faf347a44a208ec3d6eb9a48e1b97a4b568bccc5 Mon Sep 17 00:00:00 2001 From: Sergey Stepanov Date: Fri, 17 Feb 2023 18:12:38 +0300 Subject: [PATCH] Make rand.Seed not deprecated --- cmd/coordinator/main.go | 2 +- cmd/worker/main.go | 2 +- pkg/worker/compression/zip/compression_test.go | 10 ++++++++-- pkg/worker/emulator/libretro/frontend_test.go | 2 +- pkg/worker/media_test.go | 2 +- pkg/worker/recorder/recorder.go | 4 +++- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/coordinator/main.go b/cmd/coordinator/main.go index 294d6957..56da0619 100644 --- a/cmd/coordinator/main.go +++ b/cmd/coordinator/main.go @@ -13,7 +13,7 @@ import ( var Version = "?" func main() { - rand.Seed(time.Now().UTC().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) // !to remove when bumped to 1.20 conf := config.NewConfig() conf.ParseFlags() diff --git a/cmd/worker/main.go b/cmd/worker/main.go index cf3e4322..06ba6486 100644 --- a/cmd/worker/main.go +++ b/cmd/worker/main.go @@ -14,7 +14,7 @@ import ( var Version = "?" func run() { - rand.Seed(time.Now().UTC().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) // !to remove when bumped to 1.20 conf := config.NewConfig() conf.ParseFlags() diff --git a/pkg/worker/compression/zip/compression_test.go b/pkg/worker/compression/zip/compression_test.go index d0328d32..0092a267 100644 --- a/pkg/worker/compression/zip/compression_test.go +++ b/pkg/worker/compression/zip/compression_test.go @@ -1,6 +1,7 @@ package zip import ( + cr "crypto/rand" "fmt" "math/rand" "reflect" @@ -62,10 +63,15 @@ func BenchmarkCompressions(b *testing.B) { {name: "compress", size: 1024 * 1024 * 2}, } for _, bm := range benchmarks { - rand.Seed(time.Now().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) // !to remove when bumped to 1.20 b.Run(fmt.Sprintf("%v %v", bm.name, bm.size), func(b *testing.B) { + b.StopTimer() dat := make([]byte, bm.size) - rand.Read(dat) + _, err := cr.Read(dat) + if err != nil { + b.Fatal(err) + } + b.StartTimer() for i := 0; i < b.N; i++ { _, _ = Compress(dat, "test") } diff --git a/pkg/worker/emulator/libretro/frontend_test.go b/pkg/worker/emulator/libretro/frontend_test.go index 2a77e287..a3307dc0 100644 --- a/pkg/worker/emulator/libretro/frontend_test.go +++ b/pkg/worker/emulator/libretro/frontend_test.go @@ -167,7 +167,7 @@ func TestStateConcurrency(t *testing.T) { }) mock.handleAudio(func(_ *emulator.GameAudio) {}) - rand.Seed(int64(test.seed)) + rand.New(rand.NewSource(int64(test.seed))) // !to remove when bumped to 1.20 t.Logf("Random seed is [%v]\n", test.seed) t.Logf("Save path is [%v]\n", mock.paths.save) diff --git a/pkg/worker/media_test.go b/pkg/worker/media_test.go index c4e772c0..3c74c7c0 100644 --- a/pkg/worker/media_test.go +++ b/pkg/worker/media_test.go @@ -138,7 +138,7 @@ func BenchmarkResampler(b *testing.B) { } func gen(l int) []int16 { - rand.Seed(time.Now().Unix()) + rand.New(rand.NewSource(time.Now().UnixNano())) // !to remove when bumped to 1.20 nums := make([]int16, l) for i := range nums { diff --git a/pkg/worker/recorder/recorder.go b/pkg/worker/recorder/recorder.go index 7527d16b..186ecd73 100644 --- a/pkg/worker/recorder/recorder.go +++ b/pkg/worker/recorder/recorder.go @@ -65,7 +65,9 @@ type ( } ) -func init() { rand.Seed(time.Now().UnixNano()) } +func init() { + rand.New(rand.NewSource(time.Now().UnixNano())) // !to remove when bumped to 1.20 +} // NewRecording creates new media recorder for the emulator. func NewRecording(meta Meta, log *logger.Logger, opts Options) *Recording {