[#851] util/rand: use single random source

It is much more convenient to skip source creation.
Also fix some bugs:
1. `cryptoSource.Int63()` now returns number in [0, 1<<63) as required
   by `rand.Source` interface.
2. Replace `cryptoSource.Uint63()` with `cryptoSource.Uint64` to allow
   generate uint64 numbers directly (see rand.Source64 docs).

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-11 17:07:59 +03:00 committed by Alex Vanin
parent c35cdb3684
commit 5828f43e52
8 changed files with 56 additions and 66 deletions

View file

@ -4,12 +4,11 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/util/rand"
)
// returns random uint64 number [0; n) outside exclude map.
// exclude must contain no more than n-1 elements [0; n)
// nextRandUint64 returns random uint64 number [0; n) outside exclude map.
// Panics if len(exclude) >= n.
func nextRandUint64(n uint64, exclude map[uint64]struct{}) uint64 {
ln := uint64(len(exclude))
ind := randUint64(n - ln)
ind := rand.Uint64() % (n - ln)
for i := ind; ; i++ {
if _, ok := exclude[i]; !ok {
@ -17,8 +16,3 @@ func nextRandUint64(n uint64, exclude map[uint64]struct{}) uint64 {
}
}
}
// returns random uint64 number [0, n).
func randUint64(n uint64) uint64 {
return rand.Uint64(rand.New(), int64(n))
}