alist/pkg/utils/random/random.go

33 lines
554 B
Go
Raw Normal View History

2022-06-25 13:34:44 +00:00
package random
2022-06-25 12:38:02 +00:00
import (
2022-07-12 06:01:43 +00:00
"github.com/google/uuid"
2022-06-25 12:38:02 +00:00
"math/rand"
"time"
)
var Rand *rand.Rand
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
2022-06-26 11:09:28 +00:00
func String(n int) string {
2022-06-25 12:38:02 +00:00
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[Rand.Intn(len(letterBytes))]
}
return string(b)
}
2022-06-28 06:18:10 +00:00
func Token() string {
2022-07-12 06:01:43 +00:00
return uuid.NewString() + String(64)
2022-06-28 06:18:10 +00:00
}
2022-07-19 11:55:54 +00:00
func RangeInt64(left, right int64) int64 {
return rand.Int63n(left+right) - left
}
2022-06-25 12:38:02 +00:00
func init() {
s := rand.NewSource(time.Now().UnixNano())
Rand = rand.New(s)
}