Files
web_huakui_server/pub/common/random/number.go
T
rootandClaude Opus 4.7 85c0071fd8 feat: 从黄果 NET hgdjnet-server 9bfbafe 导入 HKDJ 官网后端
来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83
项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet
测试目录 /server/hkdj_net,App/Web API 30331/30332

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-10-16 11:20:04 +08:00

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)
}