random

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package random is the generator of the format and its places of randomness, written from the constants of the format's text rather than taken from math/rand/v2, with an error on an argument outside a function's domain (format/README.md, section 22; R59 spec; R10). Nothing outside module-kit imports it: what goes out is the instance of an attempt, from package template.

A Stream is the generator, started from two words or from a place: a seed and its names. It gives numbers through five functions, those the vectors of the generator check, and through nothing else: no shuffle of a list, which module-kit does not do. The generator keeps no state of its own: a stream lives in the one call that builds it. math/rand/v2 and hash/fnv are the reference in its tests alone, and no randomness comes from anything but a seed.

Index

Constants

View Source
const MaxPermutation = math.MaxInt32

MaxPermutation is the greatest count that Permutation orders, 2^31 − 1.

Variables

View Source
var ErrDomain = errors.New("outside the domain")

ErrDomain is what a function gives on an argument outside its domain, and a place on a name that is none: the function draws no integer, and no place is built (R10).

Functions

func Words

func Words(seed uint64, names []string) (high, low uint64, err error)

Words gives the two words that start the stream of a place of randomness: the high one FNV-1a of 64 bits of the UTF-8 text of the seed in decimal digits and then, for each name in order, | and the name; the low one the seed itself. A place has a name at least, and a name is UTF-8 that is not empty and holds no |; names compare as written, with no normalization. Any other list of names gives ErrDomain.

Types

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream is the generator, PCG with 128 bits of state, its state the number high · 2^64 + low. It gives numbers only through its five functions, each drawing as many integers as the format's text says. A Stream is not for two goroutines at once: each use builds its own, and lives in one call.

func New

func New(high, low uint64) *Stream

New gives the stream that starts from two words, taken as its state as they are.

func Place

func Place(seed uint64, names ...string) (*Stream, error)

Place gives the stream of the place of randomness of a seed and its names, as Words starts it.

func (*Stream) Below

func (s *Stream) Below(b uint64) (uint64, error)

Below gives an integer from 0 to b − 1, for a bound b from 1 to 2^64 − 1. A bound that is a power of two masks the next integer, which draws one; any other takes the high word of the product of the next integer and b, by Lemire's method, and takes the product again with the next integer while its low word is less than (2^64 − b) mod b, each product drawing one.

func (*Stream) Between

func (s *Stream) Between(a, c int64) (int64, error)

Between gives a + below(c − a + 1), an integer from a to c, both included, for signed integers a ≤ c with c − a + 1 at most 2^63 − 1. It checks a ≤ c first, so that c − a, taken in the words of 64 bits that wrap, is the count less one and overflows nothing.

func (*Stream) Fraction

func (s *Stream) Fraction() float64

Fraction gives a number from 0 up to 1, never 1: the low 53 bits of the next integer, divided by 2^53, which a double holds exactly. It draws one integer.

func (*Stream) Integer

func (s *Stream) Integer() uint64

Integer gives the next integer of the stream, from 0 to 2^64 − 1, and draws one: a step sets the state to state · M + C modulo 2^128, and DXSM draws the integer from the new state's words.

func (*Stream) Permutation

func (s *Stream) Permutation(n int) ([]int, error)

Permutation gives an order of the integers from 0 to n − 1, for n from 0 to 2^31 − 1: from them in order, for i from n − 1 down to 1, it swaps the items at places i and below(i + 1). It draws nothing when n is 0 or 1.

Jump to

Keyboard shortcuts

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