From b3008a51c7f68af2593bf26652f5fad4bdbd9475 Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Mon, 1 Jan 2024 14:16:20 +0000 Subject: [PATCH] tumblrandom: use better random --- go/tumblrandom/tumblrandom.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/go/tumblrandom/tumblrandom.go b/go/tumblrandom/tumblrandom.go index cc4163d1dc..f6ec2ae4e6 100644 --- a/go/tumblrandom/tumblrandom.go +++ b/go/tumblrandom/tumblrandom.go @@ -12,7 +12,7 @@ import ( "io" "io/fs" "log" - mathrand "math/rand" + "math/big" "net" "net/http" "os" @@ -289,7 +289,12 @@ func (a *app) random(rw http.ResponseWriter, r *http.Request) { return } - n := mathrand.Intn(len(u.Likes)) + bigN, err := rand.Int(rand.Reader, big.NewInt(int64(len(u.Likes)))) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } + n := int(bigN.Int64()) l := u.Likes[n] http.Redirect(rw, r, l.PostURL, http.StatusSeeOther) }