Documentation ¶
Overview ¶
Package rand is a wrapper around `crypto/rand` that uses the system RNG underneath to extract secure entropy.
It implements useful tools that are not exported by the `crypto/rand` package. This package should be used instead of `math/rand` for any use-case requiring a secure randomness. It provides similar APIs to the ones provided by `math/rand`. This package does not implement any determinstic RNG (Pseudo-RNG) based on user input seeds. For the deterministic use-cases please use `github.com/onflow/crypto/random`.
Functions in this package may return an error if the underlying system implementation fails to read new randoms. When that happens, this package considers it an irrecoverable exception.
Index ¶
- func Samples(n uint, m uint, swap func(i, j uint)) error
- func Shuffle(n uint, swap func(i, j uint)) error
- func Uint() (uint, error)
- func Uint32() (uint32, error)
- func Uint32n(n uint32) (uint32, error)
- func Uint64() (uint64, error)
- func Uint64n(n uint64) (uint64, error)
- func Uintn(n uint) (uint, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Samples ¶
Samples picks randomly `m` elements out of `n` elements in a data structure and places them in random order at indices [0,m-1], the swapping being implemented in place. The data structure is defined by the `swap` function itself. Sampling is not deterministic like the other functions of the package.
It implements the first `m` elements of Fisher-Yates Shuffle using crypto/rand as a source of randoms. `m` has to be less or equal to `n`. It uses O(1) space and O(m) time
It returns:
- (exception) if `n < m`
- (exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (nil) otherwise
func Shuffle ¶
Shuffle permutes a data structure in place based on the provided `swap` function. It is not deterministic.
It implements Fisher-Yates Shuffle using crypto/rand as a source of randoms. It uses O(1) space and O(n) time
It returns:
- (exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (nil) otherwise
func Uint ¶
Uint returns a random uint.
It returns:
- (0, exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (random, nil) otherwise
func Uint32 ¶
Uint32 returns a random uint32.
It returns:
- (0, exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (random, nil) otherwise
func Uint32n ¶
Uint32n returns a random uint32 strictly less than `n`. `n` has to be a strictly positive integer.
It returns an error:
- (0, exception) if `n==0`
- (0, exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (random, nil) otherwise
func Uint64 ¶
Uint64 returns a random uint64.
It returns:
- (0, exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (random, nil) otherwise
func Uint64n ¶
Uint64n returns a random uint64 strictly less than `n`. `n` has to be a strictly positive integer.
It returns:
- (0, exception) if `n==0`
- (0, exception) if crypto/rand fails to provide entropy which is likely a result of a system error.
- (random, nil) otherwise
Types ¶
This section is empty.