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/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)
}