rand

package
v0.31.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2023 License: AGPL-3.0 Imports: 3 Imported by: 0

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 `flow-go/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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Samples

func Samples(n uint, m uint, swap func(i, j uint)) error

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

func Shuffle(n uint, swap func(i, j uint)) error

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

func Uint() (uint, error)

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

func Uint32() (uint32, error)

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

func Uint32n(n uint32) (uint32, error)

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

func Uint64() (uint64, error)

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

func Uint64n(n uint64) (uint64, error)

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

func Uintn

func Uintn(n uint) (uint, error)

returns a random uint 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

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL