Documentation ¶
Index ¶
Constants ¶
const ( INT63_MASK = (1 << 63) - 1 // Largest prime (3 mod 4) less than 2^64, permutes [0, 2^64-189) DEFAULT_PRIME = uint64(math.MaxUint64 - 188) DEFAULT_INTERMEDIATE_OFFSET = 5_577_006_791_947_779_410 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type QuadraticResiduePRNG ¶
type QuadraticResiduePRNG struct {
// contains filtered or unexported fields
}
QuadraticResiduePRNG is a thread-unsafe PRNG based on Preshing's method using quadratic residues. The PRNG has the unique advantage of generating a permutation: when `offset` is 0, the output will cycle through all numbers less than `prime` without repeats (until all have been output and the cycle restarts). It implements both rand.Source and rand.Source64, and can be used via rand.New() to generate various random data.
func Default ¶
func Default() *QuadraticResiduePRNG
Default returns a new PRNG instance suitable for general-purpose use. It uses the largest possible prime to permute 99.999999999999999% of possible uint64 values.
func New ¶
func New(prime, intermediateOffset, offset uint64) (*QuadraticResiduePRNG, error)
New creates a new PRNG instance with the given parameters, which are validated for correctness before creation. The chosen prime must be 3 mod 4, and the intermediate offset (seed) can be any number less than the prime. The offset will be added to all output, effectively placing a floor on the output values.
func (*QuadraticResiduePRNG) Index ¶
func (prng *QuadraticResiduePRNG) Index(i uint64) (uint64, error)
Index generates the ith element of the permutation described by the generator. If i >= prime, then an error is returned. However, it can be ignored if desired; the sequence will simply cycle.
func (*QuadraticResiduePRNG) Int63 ¶
func (prng *QuadraticResiduePRNG) Int63() int64
func (*QuadraticResiduePRNG) Seed ¶
func (prng *QuadraticResiduePRNG) Seed(seed int64)
Seed changes the seed of the PRNG instance and resets the internal state of the generator.
func (*QuadraticResiduePRNG) Uint64 ¶
func (prng *QuadraticResiduePRNG) Uint64() uint64