samber / lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
https://pkg.go.dev/github.com/samber/lo
MIT License
17.95k stars 828 forks source link

The random string function always return the same value #293

Closed nam-truong-le closed 1 year ago

nam-truong-le commented 1 year ago

The function returns different values in sequential calls but the initial value is always the same:

// You can edit this code!
// Click here and start typing.
package main

import (
    "fmt"

    "github.com/samber/lo"
)

func main() {
    result1 := lo.RandomString(5, lo.LettersCharset)
    result2 := lo.RandomString(5, lo.LettersCharset)

    fmt.Printf("%v %v", result1, result2)
}

Do I need to provide any seed value?

mprimeaux commented 1 year ago

Based on this reference to this playground, it doesn't seem so.

The first run generated pubyc and the second run generated CIhQr.

samber commented 1 year ago

Since go 1.20, rand.Seed(...) is executed automatically at program startup.

https://pkg.go.dev/math/rand#Seed