10 strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
16 for i := 0; i < 8; i++ {
20 rand.Seed(int64(seed))
23 // Constructs an alphanumeric string of given length.
24 func RandStr(length int) string {
29 for i := 0; i < 10; i++ {
30 v := int(val & 0x3f) // rightmost 6 bits
31 if v >= 62 { // only 62 characters in strChars
35 chars = append(chars, strChars[v])
36 if len(chars) == length {
47 func RandUint16() uint16 {
48 return uint16(rand.Uint32() & (1<<16 - 1))
51 func RandUint32() uint32 {
55 func RandUint64() uint64 {
56 return uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
59 func RandUint() uint {
60 return uint(rand.Int())
63 func RandInt16() int16 {
64 return int16(rand.Uint32() & (1<<16 - 1))
67 func RandInt32() int32 {
68 return int32(rand.Uint32())
71 func RandInt64() int64 {
72 return int64(rand.Uint32())<<32 + int64(rand.Uint32())
79 // Distributed pseudo-exponentially to test for various cases
80 func RandUint16Exp() uint16 {
81 bits := rand.Uint32() % 16
85 n := uint16(1 << (bits - 1))
86 n += uint16(rand.Int31()) & ((1 << (bits - 1)) - 1)
90 // Distributed pseudo-exponentially to test for various cases
91 func RandUint32Exp() uint32 {
92 bits := rand.Uint32() % 32
96 n := uint32(1 << (bits - 1))
97 n += uint32(rand.Int31()) & ((1 << (bits - 1)) - 1)
101 // Distributed pseudo-exponentially to test for various cases
102 func RandUint64Exp() uint64 {
103 bits := rand.Uint32() % 64
107 n := uint64(1 << (bits - 1))
108 n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
112 func RandFloat32() float32 {
113 return rand.Float32()
116 func RandTime() time.Time {
117 return time.Unix(int64(RandUint64Exp()), 0)
120 func RandBytes(n int) []byte {
121 bs := make([]byte, n)
122 for i := 0; i < n; i++ {
123 bs[i] = byte(rand.Intn(256))
128 // NOTE: This relies on the os's random number generator.
129 // For real security, we should salt that with some seed.
130 // See github.com/tendermint/go-crypto for a more secure reader.
131 func cRandBytes(numBytes int) []byte {
132 b := make([]byte, numBytes)
133 _, err := crand.Read(b)