Golang Daily - Common

Generates a random number

func RandStringRunes(n int) string {
    // 数字/小写字母/大写字母
    var letterRunes = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

    rand.Seed(time.Now().UnixNano())
    b := make([]rune, n)
    for i := range b {
        b[i] = letterRunes[rand.Intn(len(letterRunes))]
    }
    return string(b)
}

rand.Seed - Fill the random number seed, to generate the same seed as a random number
rand.Intn - rand.Intn (n int) obtained random number int i, 0 <= i < n

Note: You can use more than go test test code, but go playground invalid.

Guess you like

Origin www.cnblogs.com/neen/p/11583657.html