Documentation
¶
Overview ¶
Package prng provides the keyed random streams every generated value is drawn from.
There is no global generator. Each consumer derives its own key from the run's root key and a path of names (the operation's identity, the field, the token), and draws from a ChaCha8 stream seeded with that key. So a value depends only on the seed and its own name: adding, removing or reordering an unrelated operation, field or token never changes it.
The mapping from stream output to values (text, hex, integers in a range, UUIDs) is defined here rather than taken from math/rand, whose derived methods are not promised to stay stable across Go releases. ChaCha8's own output is fixed by its specification (c2sp.org/chacha8rand).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key [32]byte
Key identifies one stream.
func (Key) Derive ¶
Derive returns the key for the named child of k. Parts are length-prefixed, so ("ab", "c") and ("a", "bc") name different children.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is a sequence of random values. It is not safe for concurrent use; give each goroutine its own key instead.
func (*Stream) Bytes ¶
Bytes returns n bytes: each Uint64, little-endian, in turn. The unused bytes of the last word are discarded.
func (*Stream) IntN ¶
IntN returns a uniform integer in [0, n), by rejection sampling. It panics if n <= 0.
func (*Stream) Text ¶
Text returns n characters from the base32 alphabet, one per byte (the low five bits, which are uniform).