Documentation
¶
Overview ¶
Package rand implements a fast userspace CSPRNG that is periodically reseeded with entropy obtained from crypto/rand. The PRNG can be used to obtain random bytes as well as generating uniformly-distributed integers in a full or limited range.
The default global PRNG will never panic after package init and is safe for concurrent access. Additional PRNGs which avoid the locking overhead can be created by calling NewPRNG.
Index ¶
- func BigInt(max *big.Int) *big.Int
- func Duration(n time.Duration) time.Duration
- func Int() int
- func Int32() int32
- func Int32N(n int32) int32
- func Int64() int64
- func Int64N(n int64) int64
- func IntN(n int) int
- func N[Int intType](n Int) Int
- func Read(b []byte)
- func Reader() io.Reader
- func Shuffle(n int, swap func(i, j int))
- func ShuffleSlice[S ~[]E, E any](s S)
- func Uint32() uint32
- func Uint32N(n uint32) uint32
- func Uint64() uint64
- func Uint64N(n uint64) uint64
- func UintN(n uint) uint
- type PRNG
- func (p *PRNG) BigInt(max *big.Int) *big.Int
- func (p *PRNG) Duration(n time.Duration) time.Duration
- func (p *PRNG) Int() int
- func (p *PRNG) Int32() int32
- func (p *PRNG) Int32N(n int32) int32
- func (p *PRNG) Int64() int64
- func (p *PRNG) Int64N(n int64) int64
- func (p *PRNG) IntN(n int) int
- func (p *PRNG) Read(s []byte) (n int, err error)
- func (p *PRNG) Shuffle(n int, swap func(i, j int))
- func (p *PRNG) Uint32() uint32
- func (p *PRNG) Uint32N(n uint32) uint32
- func (p *PRNG) Uint64() uint64
- func (p *PRNG) Uint64N(n uint64) uint64
- func (p *PRNG) UintN(n uint) uint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Int32 ¶
func Int32() int32
Int32 returns a random 31-bit non-negative integer as an int32 without modulo bias.
func Int32N ¶
Int32N returns, as an int32, a random 31-bit non-negative integer in [0,n) without modulo bias. Panics if n <= 0.
func Int64 ¶
func Int64() int64
Int64 returns a random 63-bit non-negative integer as an int64 without modulo bias.
func Int64N ¶
Int64N returns, as an int64, a random 63-bit non-negative integer in [0,n) without modulo bias. Panics if n <= 0.
func IntN ¶
IntN returns, as an int, a random non-negative integer in [0,n) without modulo bias. Panics if n <= 0.
func N ¶
func N[Int intType](n Int) Int
N returns a pseudo-random number in the half-open interval [0,n). The type parameter Int can be any integer type. Panics if n <= 0.
func Read ¶
func Read(b []byte)
Read fills b with random bytes obtained from the default userspace PRNG.
func Reader ¶
Reader returns the default cryptographically secure userspace PRNG that is periodically reseeded with entropy obtained from crypto/rand. The returned Reader is safe for concurrent access.
func Shuffle ¶
Shuffle randomizes the order of n elements by swapping the elements at indexes i and j. Panics if n < 0.
func ShuffleSlice ¶
func ShuffleSlice[S ~[]E, E any](s S)
ShuffleSlice randomizes the order of all elements in s.
Types ¶
type PRNG ¶
type PRNG struct {
// contains filtered or unexported fields
}
PRNG is a cryptographically secure pseudorandom number generator capable of generating random bytes and integers. PRNG methods are not safe for concurrent access.
func (*PRNG) Duration ¶
Duration returns a random duration in [0,n) without modulo bias. Panics if n <= 0.
func (*PRNG) Int32 ¶
Int32 returns a random 31-bit non-negative integer as an int32 without modulo bias.
func (*PRNG) Int32N ¶
Int32N returns, as an int32, a random 31-bit non-negative integer in [0,n) without modulo bias. Panics if n <= 0.
func (*PRNG) Int64 ¶
Int64 returns a random 63-bit non-negative integer as an int64 without modulo bias.
func (*PRNG) Int64N ¶
Int64N returns, as an int64, a random 63-bit non-negative integer in [0,n) without modulo bias. Panics if n <= 0.
func (*PRNG) IntN ¶
IntN returns, as an int, a random non-negative integer in [0,n) without modulo bias. Panics if n <= 0.
func (*PRNG) Read ¶
Read fills s with len(s) of cryptographically-secure random bytes. Read never errors.
func (*PRNG) Shuffle ¶
Shuffle randomizes the order of n elements by swapping the elements at indexes i and j. Panics if n < 0.