From 18a6d6d4e6245522094fd0358ea8e0b6cc5278bf Mon Sep 17 00:00:00 2001 From: Brandur Date: Sun, 15 Dec 2024 12:44:02 -0800 Subject: [PATCH] Switch to `math/rand/v2` Switch to `math/rand/v2` given that Go 1.21 was dropped quite some time ago. V2 has the major benefit of seeding itself, meaning we can't forget to seed it, which might be currently happening as I can't seem to find the place where rand is being seeded anywhere. --- build.go | 4 ++-- modules/stemplate/stemplate.go | 9 ++------- modules/stemplate/stemplate_test.go | 4 ---- views/sequences/_entry.tmpl.html | 12 ++++++------ 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/build.go b/build.go index 493ae2e1f..84b57a03d 100644 --- a/build.go +++ b/build.go @@ -11,7 +11,7 @@ import ( "html/template" "io" "math/big" - "math/rand" + "math/rand/v2" "net/http" "net/url" "os" @@ -2963,7 +2963,7 @@ func selectRandomPhoto(photos []*Photo) *Photo { } //nolint:gosec - return randomPhotos[rand.Intn(len(randomPhotos))] + return randomPhotos[rand.IntN(len(randomPhotos))] } // Gets a pointer to a tag just to work around the fact that you can take the diff --git a/modules/stemplate/stemplate.go b/modules/stemplate/stemplate.go index 32146dd9f..8bbbfa2aa 100644 --- a/modules/stemplate/stemplate.go +++ b/modules/stemplate/stemplate.go @@ -6,7 +6,7 @@ import ( "fmt" "html/template" "math" - "math/rand" + "math/rand/v2" "net/url" "os" "path/filepath" @@ -33,7 +33,7 @@ var FuncMap = template.FuncMap{ "NanoglyphSignup": nanoglyphSignup, "NumberWithDelimiter": numberWithDelimiter, "Pace": pace, - "RandIntn": randIntn, + "RandIntN": rand.IntN, "RenderPublishingInfo": renderPublishingInfo, "RetinaImageAlt": RetinaImageAlt, "Sub": sub, @@ -222,11 +222,6 @@ func pace(distance float64, duration time.Duration) string { return fmt.Sprintf("%v:%02d", minutes, seconds) } -func randIntn(bound int) int { - //nolint:gosec - return rand.Intn(bound) -} - func renderPublishingInfo(info map[string]string) template.HTML { s := "" diff --git a/modules/stemplate/stemplate_test.go b/modules/stemplate/stemplate_test.go index 08c6fb716..924da73a1 100644 --- a/modules/stemplate/stemplate_test.go +++ b/modules/stemplate/stemplate_test.go @@ -117,10 +117,6 @@ func TestPace(t *testing.T) { assert.Equal(t, "7:31", pace(133.0, d)) } -func TestRandIntn(t *testing.T) { - assert.Equal(t, 0, randIntn(1)) -} - func TestRetinaImageAlt(t *testing.T) { assert.Equal(t, `alt text
{{- with (index .Entry.Photos 0) -}} - {{LazyRetinaImageLightbox (RandIntn 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} + {{LazyRetinaImageLightbox (RandIntN 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} {{- end -}}
{{- with (index .Entry.Photos 1) -}} - {{LazyRetinaImageLightbox (RandIntn 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} + {{LazyRetinaImageLightbox (RandIntN 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} {{- end -}}
{{- with (index .Entry.Photos 2) -}} - {{LazyRetinaImageLightbox (RandIntn 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} + {{LazyRetinaImageLightbox (RandIntN 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} {{- end -}}
@@ -40,13 +40,13 @@
{{- with (index .Entry.Photos 0) -}} - {{LazyRetinaImageLightbox (RandIntn 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} + {{LazyRetinaImageLightbox (RandIntN 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} {{- end -}}
{{- with (index .Entry.Photos 1) -}} - {{LazyRetinaImageLightbox (RandIntn 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} + {{LazyRetinaImageLightbox (RandIntN 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} {{- end -}}
@@ -55,7 +55,7 @@
{{- with (index .Entry.Photos 0) -}} - {{LazyRetinaImageLightbox (RandIntn 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} + {{LazyRetinaImageLightbox (RandIntN 100) "/photographs/sequences/" (printf "%s_large" .Slug) .TargetExt .Portrait $linkOverride}} {{- end -}}