54 lines
942 B
Go
54 lines
942 B
Go
package random
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
const letterBytesAll = "0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().Unix())
|
|
}
|
|
|
|
func Int(n int) int {
|
|
return rand.Intn(n)
|
|
}
|
|
|
|
func SliceRand(ids []int64) (result []int64) {
|
|
result = make([]int64, 0)
|
|
if len(ids) <= 0 {
|
|
return
|
|
}
|
|
copy := make([]int64, 0)
|
|
for _, id := range ids {
|
|
copy = append(copy, id)
|
|
}
|
|
num := Int(len(copy)) + 1
|
|
for i := 0; i < num; i++ {
|
|
index := Int(len(copy))
|
|
result = append(result, copy[index])
|
|
copy = append(copy[:index], copy[index+1:]...)
|
|
}
|
|
return
|
|
}
|
|
|
|
func RandDigits(n uint) string {
|
|
s := ""
|
|
for n != 0 {
|
|
s += fmt.Sprintf("%d", rand.Intn(10))
|
|
n--
|
|
}
|
|
return s
|
|
}
|
|
|
|
// RandStrRandrAll 生成指定位数的随机数
|
|
func RandStrRandrAll(n int) string {
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letterBytesAll[rand.Int63()%int64(len(letterBytesAll))]
|
|
}
|
|
return string(b)
|
|
}
|