1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package util
import ( "crypto/rand" "fmt" "io" "math/big" "time"
"github.com/samber/lo" )
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
func RandomString(length int) string { b := make([]byte, length) lo.ForEach(b, func(_ byte, i int) { b[i] = charset[RandomInt(len(charset))] }) return string(b) }
func RandomInt(maxExclusive int) int { maxCount := big.NewInt(int64(maxExclusive)) ret, err := rand.Int(rand.Reader, maxCount) if err != nil { panic(errMsg(err)) } return int(ret.Int64()) }
func RandomFloat(maxExclusive int) float64 { maxCount := big.NewInt(int64(maxExclusive) * 1000) ret, err := rand.Int(rand.Reader, maxCount) if err != nil { panic(errMsg(err)) } return float64(ret.Int64()) / 1000 }
func RandomBool() bool { ret, err := rand.Int(rand.Reader, big.NewInt(1)) if err != nil { panic(errMsg(err)) } return ret.Int64() == 1 }
func RandomValueMap(keys int) ValueMap { ret := ValueMap{} lo.Times(keys, func(_ int) struct{} { ret[RandomString(4)] = RandomString(10) return struct{}{} }) return ret }
func RandomBytes(size int) []byte { b := make([]byte, size) _, err := io.ReadFull(rand.Reader, b) if err != nil { panic(errMsg(err)) } return b }
func RandomDate() time.Time { from := TimeCurrentUnix() to := TimeCurrent().AddDate(2, 0, 0).Unix()
rnd, err := rand.Int(rand.Reader, big.NewInt(to-from)) if err != nil { panic(errMsg(err)) }
return time.Unix(from+rnd.Int64(), 0) }
func RandomDiffs(size int) Diffs { return lo.Times(size, func(_ int) *Diff { return &Diff{Path: RandomString(8), Old: RandomString(12), New: RandomString(12)} }) }
func errMsg(err error) string { return fmt.Sprintf("source of randomness unavailable: %+v", err.Error()) }
|