|
@ -16,7 +16,7 @@ const ( |
|
|
|
|
|
|
|
|
type Rand struct { |
|
|
type Rand struct { |
|
|
sync.Mutex |
|
|
sync.Mutex |
|
|
*mrand.Rand |
|
|
|
|
|
|
|
|
rand *mrand.Rand |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var grand *Rand |
|
|
var grand *Rand |
|
@ -43,7 +43,7 @@ func (r *Rand) init() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *Rand) reset(seed int64) { |
|
|
func (r *Rand) reset(seed int64) { |
|
|
r.Rand = mrand.New(mrand.NewSource(seed)) |
|
|
|
|
|
|
|
|
r.rand = mrand.New(mrand.NewSource(seed)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
//----------------------------------------
|
|
@ -54,79 +54,79 @@ func Seed(seed int64) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandStr(length int) string { |
|
|
func RandStr(length int) string { |
|
|
return grand.RandStr(length) |
|
|
|
|
|
|
|
|
return grand.Str(length) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint16() uint16 { |
|
|
func RandUint16() uint16 { |
|
|
return grand.RandUint16() |
|
|
|
|
|
|
|
|
return grand.Uint16() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint32() uint32 { |
|
|
func RandUint32() uint32 { |
|
|
return grand.RandUint32() |
|
|
|
|
|
|
|
|
return grand.Uint32() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint64() uint64 { |
|
|
func RandUint64() uint64 { |
|
|
return grand.RandUint64() |
|
|
|
|
|
|
|
|
return grand.Uint64() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint() uint { |
|
|
func RandUint() uint { |
|
|
return grand.RandUint() |
|
|
|
|
|
|
|
|
return grand.Uint() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandInt16() int16 { |
|
|
func RandInt16() int16 { |
|
|
return grand.RandInt16() |
|
|
|
|
|
|
|
|
return grand.Int16() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandInt32() int32 { |
|
|
func RandInt32() int32 { |
|
|
return grand.RandInt32() |
|
|
|
|
|
|
|
|
return grand.Int32() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandInt64() int64 { |
|
|
func RandInt64() int64 { |
|
|
return grand.RandInt64() |
|
|
|
|
|
|
|
|
return grand.Int64() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandInt() int { |
|
|
func RandInt() int { |
|
|
return grand.RandInt() |
|
|
|
|
|
|
|
|
return grand.Int() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandInt31() int32 { |
|
|
func RandInt31() int32 { |
|
|
return grand.RandInt31() |
|
|
|
|
|
|
|
|
return grand.Int31() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandInt63() int64 { |
|
|
func RandInt63() int64 { |
|
|
return grand.RandInt63() |
|
|
|
|
|
|
|
|
return grand.Int63() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint16Exp() uint16 { |
|
|
func RandUint16Exp() uint16 { |
|
|
return grand.RandUint16Exp() |
|
|
|
|
|
|
|
|
return grand.Uint16Exp() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint32Exp() uint32 { |
|
|
func RandUint32Exp() uint32 { |
|
|
return grand.RandUint32Exp() |
|
|
|
|
|
|
|
|
return grand.Uint32Exp() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandUint64Exp() uint64 { |
|
|
func RandUint64Exp() uint64 { |
|
|
return grand.RandUint64Exp() |
|
|
|
|
|
|
|
|
return grand.Uint64Exp() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandFloat32() float32 { |
|
|
func RandFloat32() float32 { |
|
|
return grand.RandFloat32() |
|
|
|
|
|
|
|
|
return grand.Float32() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandTime() time.Time { |
|
|
func RandTime() time.Time { |
|
|
return grand.RandTime() |
|
|
|
|
|
|
|
|
return grand.Time() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandBytes(n int) []byte { |
|
|
func RandBytes(n int) []byte { |
|
|
return grand.RandBytes(n) |
|
|
|
|
|
|
|
|
return grand.Bytes(n) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandIntn(n int) int { |
|
|
func RandIntn(n int) int { |
|
|
return grand.RandIntn(n) |
|
|
|
|
|
|
|
|
return grand.Intn(n) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RandPerm(n int) []int { |
|
|
func RandPerm(n int) []int { |
|
|
return grand.RandPerm(n) |
|
|
|
|
|
|
|
|
return grand.Perm(n) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
//----------------------------------------
|
|
@ -140,11 +140,11 @@ func (r *Rand) Seed(seed int64) { |
|
|
|
|
|
|
|
|
// Constructs an alphanumeric string of given length.
|
|
|
// Constructs an alphanumeric string of given length.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandStr(length int) string { |
|
|
|
|
|
|
|
|
func (r *Rand) Str(length int) string { |
|
|
chars := []byte{} |
|
|
chars := []byte{} |
|
|
MAIN_LOOP: |
|
|
MAIN_LOOP: |
|
|
for { |
|
|
for { |
|
|
val := r.RandInt63() |
|
|
|
|
|
|
|
|
val := r.Int63() |
|
|
for i := 0; i < 10; i++ { |
|
|
for i := 0; i < 10; i++ { |
|
|
v := int(val & 0x3f) // rightmost 6 bits
|
|
|
v := int(val & 0x3f) // rightmost 6 bits
|
|
|
if v >= 62 { // only 62 characters in strChars
|
|
|
if v >= 62 { // only 62 characters in strChars
|
|
@ -164,127 +164,127 @@ MAIN_LOOP: |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint16() uint16 { |
|
|
|
|
|
return uint16(r.RandUint32() & (1<<16 - 1)) |
|
|
|
|
|
|
|
|
func (r *Rand) Uint16() uint16 { |
|
|
|
|
|
return uint16(r.rand.Uint32() & (1<<16 - 1)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint32() uint32 { |
|
|
|
|
|
|
|
|
func (r *Rand) Uint32() uint32 { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
u32 := r.Uint32() |
|
|
|
|
|
|
|
|
u32 := r.rand.Uint32() |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return u32 |
|
|
return u32 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint64() uint64 { |
|
|
|
|
|
return uint64(r.RandUint32())<<32 + uint64(r.RandUint32()) |
|
|
|
|
|
|
|
|
func (r *Rand) Uint64() uint64 { |
|
|
|
|
|
return uint64(r.rand.Uint32())<<32 + uint64(r.rand.Uint32()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint() uint { |
|
|
|
|
|
|
|
|
func (r *Rand) Uint() uint { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
i := r.Int() |
|
|
|
|
|
|
|
|
i := r.rand.Int() |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return uint(i) |
|
|
return uint(i) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandInt16() int16 { |
|
|
|
|
|
return int16(r.RandUint32() & (1<<16 - 1)) |
|
|
|
|
|
|
|
|
func (r *Rand) Int16() int16 { |
|
|
|
|
|
return int16(r.rand.Uint32() & (1<<16 - 1)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandInt32() int32 { |
|
|
|
|
|
return int32(r.RandUint32()) |
|
|
|
|
|
|
|
|
func (r *Rand) Int32() int32 { |
|
|
|
|
|
return int32(r.rand.Uint32()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandInt64() int64 { |
|
|
|
|
|
return int64(r.RandUint64()) |
|
|
|
|
|
|
|
|
func (r *Rand) Int64() int64 { |
|
|
|
|
|
return int64(r.rand.Uint64()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandInt() int { |
|
|
|
|
|
|
|
|
func (r *Rand) Int() int { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
i := r.Int() |
|
|
|
|
|
|
|
|
i := r.rand.Int() |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return i |
|
|
return i |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandInt31() int32 { |
|
|
|
|
|
|
|
|
func (r *Rand) Int31() int32 { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
i31 := r.Int31() |
|
|
|
|
|
|
|
|
i31 := r.rand.Int31() |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return i31 |
|
|
return i31 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandInt63() int64 { |
|
|
|
|
|
|
|
|
func (r *Rand) Int63() int64 { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
i63 := r.Int63() |
|
|
|
|
|
|
|
|
i63 := r.rand.Int63() |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return i63 |
|
|
return i63 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Distributed pseudo-exponentially to test for various cases
|
|
|
// Distributed pseudo-exponentially to test for various cases
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint16Exp() uint16 { |
|
|
|
|
|
bits := r.RandUint32() % 16 |
|
|
|
|
|
|
|
|
func (r *Rand) Uint16Exp() uint16 { |
|
|
|
|
|
bits := r.rand.Uint32() % 16 |
|
|
if bits == 0 { |
|
|
if bits == 0 { |
|
|
return 0 |
|
|
return 0 |
|
|
} |
|
|
} |
|
|
n := uint16(1 << (bits - 1)) |
|
|
n := uint16(1 << (bits - 1)) |
|
|
n += uint16(r.RandInt31()) & ((1 << (bits - 1)) - 1) |
|
|
|
|
|
|
|
|
n += uint16(r.rand.Int31()) & ((1 << (bits - 1)) - 1) |
|
|
return n |
|
|
return n |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Distributed pseudo-exponentially to test for various cases
|
|
|
// Distributed pseudo-exponentially to test for various cases
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint32Exp() uint32 { |
|
|
|
|
|
bits := r.RandUint32() % 32 |
|
|
|
|
|
|
|
|
func (r *Rand) Uint32Exp() uint32 { |
|
|
|
|
|
bits := r.rand.Uint32() % 32 |
|
|
if bits == 0 { |
|
|
if bits == 0 { |
|
|
return 0 |
|
|
return 0 |
|
|
} |
|
|
} |
|
|
n := uint32(1 << (bits - 1)) |
|
|
n := uint32(1 << (bits - 1)) |
|
|
n += uint32(r.RandInt31()) & ((1 << (bits - 1)) - 1) |
|
|
|
|
|
|
|
|
n += uint32(r.rand.Int31()) & ((1 << (bits - 1)) - 1) |
|
|
return n |
|
|
return n |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Distributed pseudo-exponentially to test for various cases
|
|
|
// Distributed pseudo-exponentially to test for various cases
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandUint64Exp() uint64 { |
|
|
|
|
|
bits := r.RandUint32() % 64 |
|
|
|
|
|
|
|
|
func (r *Rand) Uint64Exp() uint64 { |
|
|
|
|
|
bits := r.rand.Uint32() % 64 |
|
|
if bits == 0 { |
|
|
if bits == 0 { |
|
|
return 0 |
|
|
return 0 |
|
|
} |
|
|
} |
|
|
n := uint64(1 << (bits - 1)) |
|
|
n := uint64(1 << (bits - 1)) |
|
|
n += uint64(r.RandInt63()) & ((1 << (bits - 1)) - 1) |
|
|
|
|
|
|
|
|
n += uint64(r.rand.Int63()) & ((1 << (bits - 1)) - 1) |
|
|
return n |
|
|
return n |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandFloat32() float32 { |
|
|
|
|
|
|
|
|
func (r *Rand) Float32() float32 { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
f32 := r.Float32() |
|
|
|
|
|
|
|
|
f32 := r.rand.Float32() |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return f32 |
|
|
return f32 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandTime() time.Time { |
|
|
|
|
|
return time.Unix(int64(r.RandUint64Exp()), 0) |
|
|
|
|
|
|
|
|
func (r *Rand) Time() time.Time { |
|
|
|
|
|
return time.Unix(int64(r.Uint64Exp()), 0) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// RandBytes returns n random bytes from the OS's source of entropy ie. via crypto/rand.
|
|
|
// RandBytes returns n random bytes from the OS's source of entropy ie. via crypto/rand.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandBytes(n int) []byte { |
|
|
|
|
|
|
|
|
func (r *Rand) Bytes(n int) []byte { |
|
|
// cRandBytes isn't guaranteed to be fast so instead
|
|
|
// cRandBytes isn't guaranteed to be fast so instead
|
|
|
// use random bytes generated from the internal PRNG
|
|
|
// use random bytes generated from the internal PRNG
|
|
|
bs := make([]byte, n) |
|
|
bs := make([]byte, n) |
|
|
for i := 0; i < len(bs); i++ { |
|
|
for i := 0; i < len(bs); i++ { |
|
|
bs[i] = byte(r.RandInt() & 0xFF) |
|
|
|
|
|
|
|
|
bs[i] = byte(r.rand.Int() & 0xFF) |
|
|
} |
|
|
} |
|
|
return bs |
|
|
return bs |
|
|
} |
|
|
} |
|
@ -292,18 +292,18 @@ func (r *Rand) RandBytes(n int) []byte { |
|
|
// RandIntn returns, as an int, a non-negative pseudo-random number in [0, n).
|
|
|
// RandIntn returns, as an int, a non-negative pseudo-random number in [0, n).
|
|
|
// It panics if n <= 0.
|
|
|
// It panics if n <= 0.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandIntn(n int) int { |
|
|
|
|
|
|
|
|
func (r *Rand) Intn(n int) int { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
i := r.Intn(n) |
|
|
|
|
|
|
|
|
i := r.rand.Intn(n) |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return i |
|
|
return i |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// RandPerm returns a pseudo-random permutation of n integers in [0, n).
|
|
|
// RandPerm returns a pseudo-random permutation of n integers in [0, n).
|
|
|
// It is not safe for cryptographic usage.
|
|
|
// It is not safe for cryptographic usage.
|
|
|
func (r *Rand) RandPerm(n int) []int { |
|
|
|
|
|
|
|
|
func (r *Rand) Perm(n int) []int { |
|
|
r.Lock() |
|
|
r.Lock() |
|
|
perm := r.Perm(n) |
|
|
|
|
|
|
|
|
perm := r.rand.Perm(n) |
|
|
r.Unlock() |
|
|
r.Unlock() |
|
|
return perm |
|
|
return perm |
|
|
} |
|
|
} |
|
|