mercredi 28 août 2019

What Collision Ratio Of Generate-Random-String Function Is Suitable for Production

In our project there is one function which is used to generate fixed-length random string. It's code is like below.

func (r RandB) Rand(length int) string {
    bucket := "0123456789abcdefghijklmnopqrstuvwxyz"

    bytes := []byte(bucket)
    size := len(bytes)

    result := make([]byte, length)
    atomic.AddInt64(&seed, 1)

    rd := rand.New(rand.NewSource(seed))
    for i := 0; i < length; i++ {
        result[i] = bytes[rd.Intn(size)]
    }

    return string(result)
}

I accidentlly found the collision ratio for this function is about 0.0026 by average, got by 1million times of generation.

I don't think this is a reasonable value for production. So I wonder what value shall be reasonable? Can we use this function in the production environment?




Aucun commentaire:

Enregistrer un commentaire