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 <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-05 15:49:00 -05:00
parent ce8475a0b9
commit 63fea3d2bd
+6 -9
View File
@@ -7,7 +7,6 @@ import (
"os" "os"
"sort" "sort"
"testing" "testing"
"time"
colEmoji "eliasnaur.com/font/noto/emoji/color" colEmoji "eliasnaur.com/font/noto/emoji/color"
"gioui.org/font" "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)) { func runBenchmarkPermutations(b *testing.B, benchmark func(b *testing.B, runes int, locale system.Locale, document string)) {
docKeys := maps.Keys(documents) docKeys := maps.Keys(documents)
sort.Strings(docKeys) sort.Strings(docKeys)
@@ -127,11 +122,12 @@ func BenchmarkLabelDynamic(b *testing.B) {
font := font.Font{} font := font.Font{}
runes := []rune(txt)[:runeCount] runes := []rune(txt)[:runeCount]
l := Label{} l := Label{}
r := rand.New(rand.NewSource(42))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
// simulate a constantly changing string // simulate a constantly changing string
a := rand.Intn(len(runes)) a := r.Intn(len(runes))
b := rand.Intn(len(runes)) b := r.Intn(len(runes))
runes[a], runes[b] = runes[b], runes[a] runes[a], runes[b] = runes[b], runes[a]
l.Layout(gtx, cache, font, fontSize, string(runes), op.CallOp{}) l.Layout(gtx, cache, font, fontSize, string(runes), op.CallOp{})
if render { if render {
@@ -196,11 +192,12 @@ func BenchmarkEditorDynamic(b *testing.B) {
runes := []rune(txt)[:runeCount] runes := []rune(txt)[:runeCount]
e := Editor{} e := Editor{}
e.SetText(string(runes)) e.SetText(string(runes))
r := rand.New(rand.NewSource(42))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
// simulate a constantly changing string // simulate a constantly changing string
a := rand.Intn(e.Len()) a := r.Intn(e.Len())
b := rand.Intn(e.Len()) b := r.Intn(e.Len())
e.SetCaret(a, a+1) e.SetCaret(a, a+1)
takeStr := e.SelectedText() takeStr := e.SelectedText()
e.Insert("") e.Insert("")