mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-18 15:41:20 +00:00
common: use names prng and mrand
This commit is contained in:
@ -2,7 +2,7 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"math/rand"
|
mrand "math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -11,9 +11,11 @@ const (
|
|||||||
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
|
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
|
||||||
)
|
)
|
||||||
|
|
||||||
var rng struct {
|
// pseudo random number generator.
|
||||||
|
// seeded with OS randomness (crand)
|
||||||
|
var prng struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
*rand.Rand
|
*mrand.Rand
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -23,7 +25,7 @@ func init() {
|
|||||||
seed |= uint64(b[i])
|
seed |= uint64(b[i])
|
||||||
seed <<= 8
|
seed <<= 8
|
||||||
}
|
}
|
||||||
rng.Rand = rand.New(rand.NewSource(int64(seed)))
|
prng.Rand = mrand.New(mrand.NewSource(int64(seed)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructs an alphanumeric string of given length.
|
// Constructs an alphanumeric string of given length.
|
||||||
@ -31,7 +33,7 @@ func RandStr(length int) string {
|
|||||||
chars := []byte{}
|
chars := []byte{}
|
||||||
MAIN_LOOP:
|
MAIN_LOOP:
|
||||||
for {
|
for {
|
||||||
val := rng.Int63()
|
val := prng.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
|
||||||
@ -55,9 +57,9 @@ func RandUint16() uint16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RandUint32() uint32 {
|
func RandUint32() uint32 {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
u32 := rng.Uint32()
|
u32 := prng.Uint32()
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return u32
|
return u32
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,9 +68,9 @@ func RandUint64() uint64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RandUint() uint {
|
func RandUint() uint {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
i := rng.Int()
|
i := prng.Int()
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return uint(i)
|
return uint(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,23 +87,23 @@ func RandInt64() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RandInt() int {
|
func RandInt() int {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
i := rng.Int()
|
i := prng.Int()
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandInt31() int32 {
|
func RandInt31() int32 {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
i31 := rng.Int31()
|
i31 := prng.Int31()
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return i31
|
return i31
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandInt63() int64 {
|
func RandInt63() int64 {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
i63 := rng.Int63()
|
i63 := prng.Int63()
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return i63
|
return i63
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,9 +141,9 @@ func RandUint64Exp() uint64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RandFloat32() float32 {
|
func RandFloat32() float32 {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
f32 := rng.Float32()
|
f32 := prng.Float32()
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return f32
|
return f32
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,6 +151,7 @@ func RandTime() time.Time {
|
|||||||
return time.Unix(int64(RandUint64Exp()), 0)
|
return time.Unix(int64(RandUint64Exp()), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandBytes returns n random bytes from the OS's source of entropy ie. via crypto/rand.
|
||||||
func RandBytes(n int) []byte {
|
func RandBytes(n int) []byte {
|
||||||
return cRandBytes(n)
|
return cRandBytes(n)
|
||||||
}
|
}
|
||||||
@ -156,17 +159,17 @@ func 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
|
||||||
func RandIntn(n int) int {
|
func RandIntn(n int) int {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
i := rng.Intn(n)
|
i := prng.Intn(n)
|
||||||
rng.Unlock()
|
prng.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).
|
||||||
func RandPerm(n int) []int {
|
func RandPerm(n int) []int {
|
||||||
rng.Lock()
|
prng.Lock()
|
||||||
perm := rng.Perm(n)
|
perm := prng.Perm(n)
|
||||||
rng.Unlock()
|
prng.Unlock()
|
||||||
return perm
|
return perm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,9 +9,30 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tendermint/tmlibs/common"
|
"github.com/tendermint/tmlibs/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestRandStr(t *testing.T) {
|
||||||
|
l := 243
|
||||||
|
s := common.RandStr(l)
|
||||||
|
assert.Equal(t, l, len(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandBytes(t *testing.T) {
|
||||||
|
l := 243
|
||||||
|
b := common.RandBytes(l)
|
||||||
|
assert.Equal(t, l, len(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandIntn(t *testing.T) {
|
||||||
|
n := 243
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
x := common.RandIntn(n)
|
||||||
|
assert.True(t, x < n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// It is essential that these tests run and never repeat their outputs
|
// It is essential that these tests run and never repeat their outputs
|
||||||
// lest we've been pwned and the behavior of our randomness is controlled.
|
// lest we've been pwned and the behavior of our randomness is controlled.
|
||||||
// See Issues:
|
// See Issues:
|
||||||
|
Reference in New Issue
Block a user