vendredi 3 février 2023

Generate random values for golange

I need a random password generator for a project, I need to make 10 of them. I found this function (random), but when I run it in a loop, it generates the same passwords. I don't know what's the problem.

func main() {

    for i := 0; i < 10; i++ {
        a := random()
        fmt.Println(a)
    }
}

func random() string {
    rand.Seed(time.Now().UnixNano())
    chars := []rune("abcdefghijklmnopqrstuvwxyz" + "0123456789")
    length := 10
    var b strings.Builder
    for i := 0; i < length; i++ {
        b.WriteRune(chars[rand.Intn(len(chars))])
    }
    str := b.String()
    return str
}



Aucun commentaire:

Enregistrer un commentaire