init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user