func Random(strings []string, length int) (string, error) {
if len(strings) <= 0 {
return "", errors.New("the length of the parameter strings should not be less than 0")
}
if length <= 0 || len(strings) <= length {
return "", errors.New("the size of the parameter length illegal")
}
for i := len(strings) - 1; i > 0; i-- {
num := rand.Intn(i + 1)
strings, strings[num] = strings[num], strings
}
str := ""
for i := 0; i < length; i++ {
str += strings
}
return str, nil
}
无论在算法本身的执行过程中,还是生成随机数的过程,使用Fisher-Yates洗牌算法必须小心,否则就可能出现一些偏差。例如随机数生成带来的误差,造成洗牌的结果整体上不满足均匀分布的特点。
---------------------
【转载】仅作分享,侵删
作者:benben_2015
原文:https://blog.csdn.net/benben_2015/article/details/79996849