1 Star 0 Fork 0

cilidm / toolbox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rand.go 998 Bytes
一键复制 编辑 原始数据 按行查看 历史
30ops 提交于 2020-11-18 20:18 . update
package rand
import (
"math/rand"
"time"
)
// Ints returns a random integer array with the specified from, to and size.
func Ints(from, to, size int) []int {
if to-from < size {
size = to - from
}
var slice []int
for i := from; i < to; i++ {
slice = append(slice, i)
}
var ret []int
for i := 0; i < size; i++ {
idx := rand.Intn(len(slice))
ret = append(ret, slice[idx])
slice = append(slice[:idx], slice[idx+1:]...)
}
return ret
}
// String returns a random string ['a', 'z'] and ['0', '9'] in the specified length.
func String(length int) string {
rand.Seed(time.Now().UTC().UnixNano())
time.Sleep(time.Nanosecond)
letter := []rune("abcdefghijklmnopqrstuvwxyz0123456789")
b := make([]rune, length)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return string(b)
}
// Int returns a random integer in range [min, max].
func Int(min int, max int) int {
rand.Seed(time.Now().UnixNano())
time.Sleep(time.Nanosecond)
return min + rand.Intn(max-min)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cilidm/toolbox.git
git@gitee.com:cilidm/toolbox.git
cilidm
toolbox
toolbox
14c708c90a3a

搜索帮助

344bd9b3 5694891 D2dac590 5694891