From 63fea3d2bd2507259e59b4073d4fad649c96dc0d Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 5 Oct 2023 15:49:00 -0500 Subject: [PATCH] widget: use local random source to avoid deprecated rand.Seed This change replace the global rand use with a local source, to avoid the recently deprecated global rand.Seed function. At the same time, the time-dependent seeds are replaced with static numbers to ensure reproducible benchmarks numbers. Signed-off-by: Elias Naur --- widget/text_bench_test.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/widget/text_bench_test.go b/widget/text_bench_test.go index a0d8e855..34a81b71 100644 --- a/widget/text_bench_test.go +++ b/widget/text_bench_test.go @@ -7,7 +7,6 @@ import ( "os" "sort" "testing" - "time" colEmoji "eliasnaur.com/font/noto/emoji/color" "gioui.org/font" @@ -48,10 +47,6 @@ var ( }() ) -func init() { - rand.Seed(int64(time.Now().Nanosecond())) -} - func runBenchmarkPermutations(b *testing.B, benchmark func(b *testing.B, runes int, locale system.Locale, document string)) { docKeys := maps.Keys(documents) sort.Strings(docKeys) @@ -127,11 +122,12 @@ func BenchmarkLabelDynamic(b *testing.B) { font := font.Font{} runes := []rune(txt)[:runeCount] l := Label{} + r := rand.New(rand.NewSource(42)) b.ResetTimer() for i := 0; i < b.N; i++ { // simulate a constantly changing string - a := rand.Intn(len(runes)) - b := rand.Intn(len(runes)) + a := r.Intn(len(runes)) + b := r.Intn(len(runes)) runes[a], runes[b] = runes[b], runes[a] l.Layout(gtx, cache, font, fontSize, string(runes), op.CallOp{}) if render { @@ -196,11 +192,12 @@ func BenchmarkEditorDynamic(b *testing.B) { runes := []rune(txt)[:runeCount] e := Editor{} e.SetText(string(runes)) + r := rand.New(rand.NewSource(42)) b.ResetTimer() for i := 0; i < b.N; i++ { // simulate a constantly changing string - a := rand.Intn(e.Len()) - b := rand.Intn(e.Len()) + a := r.Intn(e.Len()) + b := r.Intn(e.Len()) e.SetCaret(a, a+1) takeStr := e.SelectedText() e.Insert("")