jeudi 15 mars 2018

Random number in GoLang with the time package

So I'm looking to generate a random number in Go and the recommended way of doing this is apparently to use something like this:

package main
import(
    "fmt"
    "math/rand"
    "time"
)

func random(min, max int) int {
    rand.Seed(time.Now().Unix())
    return rand.Intn(max - min) + min
}

But this seems like a solution where the random number isn't random if 2 people would run this script at the same time. The seed is apparently the time of executing and this could very well be the same in some cases.

Isn't this a pretty inaccurate way of generating a random number? I ran it a couple times and it just gives me the same numnber if I run it fast enough. Isn't there a better solution for a random number? Like for example using cryptography or something as seed? Time just doesn't seem like the best seed.




Aucun commentaire:

Enregistrer un commentaire