1234567891011121314151617181920212223242526272829303132333435363738 |
- package util
- import (
- "math/rand"
- "time"
- )
- var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
- // RandomString returns a random string with a fixed length
- func RandomString(n int, allowedChars ...[]rune) string {
- var letters []rune
- if len(allowedChars) == 0 {
- letters = defaultLetters
- } else {
- letters = allowedChars[0]
- }
- b := make([]rune, n)
- for i := range b {
- b[i] = letters[rand.Intn(len(letters))]
- }
- return string(b)
- }
- func RandInt64(min, max int64) int64 {
- s := rand.NewSource(time.Now().UnixNano())
- r := rand.New(s)
- return min + r.Int63n(max-min+1)
- }
- func RandFloat64(min, max float64) float64 {
- s := rand.NewSource(time.Now().UnixNano())
- r := rand.New(s)
- return min + r.Float64()*(max-min)
- }
|