tumblrandom: use better random

This commit is contained in:
Luke Granger-Brown 2024-01-01 14:16:20 +00:00
parent b3cd894f7c
commit b3008a51c7

View file

@ -12,7 +12,7 @@ import (
"io" "io"
"io/fs" "io/fs"
"log" "log"
mathrand "math/rand" "math/big"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -289,7 +289,12 @@ func (a *app) random(rw http.ResponseWriter, r *http.Request) {
return 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] l := u.Likes[n]
http.Redirect(rw, r, l.PostURL, http.StatusSeeOther) http.Redirect(rw, r, l.PostURL, http.StatusSeeOther)
} }