prng

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrngStateSize = 16
	XoroStateSize = 16
	XoshStateSize = 32
)

Random state sizes in bytes.

Variables

This section is empty.

Functions

func Float64

func Float64() float64

Float64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution is 2^53 evenly spaced floats with spacing 2^-53.

func Float64Bisect

func Float64Bisect(round bool) float64

Float64Bisect returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats. Float64Bisect is a slow function only for validating other functions distributions. if round is true, rounding is used.

func Float64_64

func Float64_64() float64

Float64_64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-12, 1) and 2^52 evenly spaced floats in [0, 2^-12) with spacing 2^-64.

func Float64_117

func Float64_117() float64

Float64_117 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-65, 1) and 2^52 evenly spaced floats in [0, 2^-65) with spacing 2^-117.

func Float64full

func Float64full() float64

Float64full returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-1023, 1) and 0.

func Int

func Int() int

Int returns a non-negative pseudo-random int.

func Int63

func Int63() int64

Int63 returns a non-negative pseudo-random int64.

func Int63n

func Int63n(n int64) int64

Int63n return a pseudo-random number in [0,n) as an int64

func Intn

func Intn(n int) int

Intn returns a pseudo-random number in [0,n) as an int.

func NewSource

func NewSource(seed int64) uint64

NewSource is only for math/rand compability.

func OverlapProbability

func OverlapProbability(n, L, P float64) (lower, upper float64)

OverlapProbability function calculates lower and upper bound of the probability for an event that at least two random streams overlap when splitting a single prng by random seeding. Formulas from http://vigna.di.unimi.it/ftp/papers/overlap.pdf. n = number of splitted parallel prng's L = lenght of the random stream for each prng P = full period of the prng. Normal float64 arithmetic used doesn't give enough lower lim accuracy for long full periods.

func RandomReal

func RandomReal() float64

RandomReal returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-1023, 1) and 0.

func ResetGlobalOutlet

func ResetGlobalOutlet(seed uint64)

ResetGlobalOutlet resets the globalOutlet by the seed. This can be made only once.

func Seed

func Seed(seed uint64)

Seed seeds system global generator globalPrng by seed.

func Splitmix

func Splitmix(seed *uint64) uint64

Splitmix is a 64-bit state SplitMix64 pseudo-random number generator from http://prng.di.unimi.it/splitmix64.c . The pointer parameter seed is used as the random state. Splitmix is used here to blend seeds for the other generators. It is a good and quite fast 64-bit state generator for other uses too.

func SplitmixJump

func SplitmixJump(seed *uint64, jump int64)

SplitmixJump (&seed, jump) sets the seed to the same value as jump calls to Splitmix(&seed). Jump can be negative.

func Uint64

func Uint64() uint64

Uint64 returns a pseudo-random 64-bit value as an uint64

func Uint64n

func Uint64n(n uint64) uint64

Uint64n returns a pseudo-random number in [0,n) as an uint64.

Types

type MCG

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

A MCG implements 64-bit multiplicative congruential pseudorandom number generator (MCG) modulo 2^64 with 64-bit state and maximun period of 2^62.

func NewMCG

func NewMCG(seed uint64) MCG

NewMCG --

func (*MCG) Float64

func (x *MCG) Float64() (next float64)

Float64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution is 2^53 evenly spaced floats with spacing 2^-53. Float64 uses multiplicative congruential pseudorandom number generator (MCG) mod 2^64. 53 high bits of the MCG are considered good enough for a fast float64, but they don't pass random tests for the last ~3 bits.

func (*MCG) Float64_64

func (x *MCG) Float64_64() float64

Float64_64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-12, 1) and 2^52 evenly spaced floats in [0, 2^-12) with spacing 2^-64. This function inlines ok.

func (*MCG) Lehmer64

func (x *MCG) Lehmer64() uint64

Lehmer64 is pure Lehmer generator.

func (*MCG) Seed

func (x *MCG) Seed(seed uint64)

Seed --

func (*MCG) Uint64

func (x *MCG) Uint64() (next uint64)

Uint64 returns a pseudo-random uint64 by MCG mod 2^64. The multiplier is picked from Table 6 in Steele & Vigna. Without the xor-rotate scrambler, the last bits are not uniformly distributed. This is a very fast generator, but not properly tested or proved to give anything good.

func (*MCG) Uint64Mul

func (x *MCG) Uint64Mul() (next uint64)

Uint64Mul uses 128-bit multiplication and the high bits of it.

type Outlet

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

Outlet is a delivery type of pseudo-random number generators with non-overlapping random streams. Methods of Outlet use sync.Mutex to protect the Outlet for simultaneous access.

func NewOutlet

func NewOutlet(seed uint64) *Outlet

NewOutlet returns a new generator delivery Outlet seeded by the seed.

func (*Outlet) Next

func (s *Outlet) Next() Prng

Next returns the next rng from Outlet. Each rng has 2^64 long random stream, which is not overlapping with other Rands streams. Next is safe for concurrent use by multiple goroutines.

func (*Outlet) NextXoro

func (s *Outlet) NextXoro() Xoro

NextXoro returns the next xoroshiro128 from Outlet. Each generator has 2^64 long random streams, which is not overlapping with other generators streams. NextXoro is safe for concurrent use by multiple goroutines.

func (*Outlet) NextXosh

func (s *Outlet) NextXosh() Xosh

NextXosh returns the next xoshiro256 generator from Outlet. Each generator has 2^128 long random streams, which is not overlapping with other generators streams. NextXosh is safe for concurrent use by multiple goroutines.

type Prng

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

A Prng is a wrapper around the actual pseudo-random number generator. It is now fixed to xoroshiro128 generator instead of having more flexible rng interface. This way we get faster inlineable functions, but cannot change the rng in an application.

func New

func New(seed uint64) Prng

New returns a new Prng seeded with the seed.

func NewPrngSlice

func NewPrngSlice(n int, seed uint64) []Prng

NewPrngSlice returns a slice of n Rands with non-overlapping random streams. The first Prng is seeded by seed.

func Next

func Next() Prng

Next returns the next non-overlapping stream Prng from globalOutlet. Next is safe for concurrent use by multiple goroutines.

func (*Prng) Float64

func (r *Prng) Float64() float64

Float64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes 2^53 evenly spaced floats with spacing 2^-53.

func (*Prng) Float64Bisect

func (r *Prng) Float64Bisect(round bool) float64

Float64Bisect returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats. Float64Bisect is a slow function only for validating other functions distributions.

func (*Prng) Float64_64

func (r *Prng) Float64_64() float64

Float64_64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-12, 1) and 2^52 evenly spaced floats in [0, 2^-12) with spacing 2^-64.

func (*Prng) Float64_117

func (r *Prng) Float64_117() float64

Float64_117 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-65, 1) and 2^52 evenly spaced floats in [0, 2^-65) with spacing 2^-117.

func (*Prng) Float64full

func (r *Prng) Float64full() float64

Float64full returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [0, 1).

func (*Prng) Int

func (r *Prng) Int() int

Int returns a non-negative pseudo-random int.

func (*Prng) Int63

func (r *Prng) Int63() int64

Int63 returns a non-negative pseudo-random int64.

func (*Prng) Int63n

func (r *Prng) Int63n(n int64) int64

Int63n return a pseudo-random number in [0,n) as an int64.

func (*Prng) Intn

func (r *Prng) Intn(n int) int

Intn returns a pseudo-random number in [0,n) as an int.

func (*Prng) Jump

func (r *Prng) Jump()

Jump sets r to the same state as 2^64 calls to r.Uint64. Jump can be used to generate 2^64 non-overlapping subsequences for parallel computation.

func (*Prng) RandomReal

func (r *Prng) RandomReal() float64

RandomReal returns a uniformly distributed pseudo-random float64 from [0, 1]. The distribution includes all floats in [0, 1]. http://prng.di.unimi.it/random_real.c

func (*Prng) ReadState

func (r *Prng) ReadState(b []byte)

ReadState reads the state of the generator r from b []byte. r.ReadState(r.State()) changes nothing.

func (*Prng) Seed

func (r *Prng) Seed(seed uint64)

Seed seeds a Prng by the seed. Any seed is ok. Do not seed Rands created by Next or NewPrngSlice.

func (*Prng) State

func (r *Prng) State() []byte

State returns the current state of the generator r as []byte.

func (*Prng) Uint64

func (r *Prng) Uint64() uint64

Uint64 returns a pseudo-random uint64.

func (*Prng) Uint64n

func (r *Prng) Uint64n(n uint64) uint64

Uint64n returns a pseudo-random number in [0,n) as an uint64. Uint64n doesn't make any bias correction. The bias with 64-bit numbers is very small and propably not detectable from the random stream.

func (*Prng) WriteState

func (r *Prng) WriteState(b []byte)

WriteState writes the state of the generator r to b []byte.

type Xoro

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

A Xoro with a xoroshiro prng implements a 64-bit generator with 128-bit state. A Xoro is the den of the xoroshiros holding their two 64-bit eggs.

func NewXoro

func NewXoro(seed uint64) Xoro

NewXoro returns a new xoroshiro128 generator seeded by the seed. Float64 uses xoroshiro128+ and Uint64 xoroshiro128**. Both xoroshiros update a Xoro in the same way (same linear engine) and we can use a single Xoro for both functions without interfering random stream properties.

func NewXoroSlice

func NewXoroSlice(n int, seed uint64) []Xoro

NewXoroSlice returns a slice of n xoroshiro128 generators with non-overlapping 2^64 long random streams. First generator is seeded by seed.

func NextXoro

func NextXoro() Xoro

NextXoro returns the next non-overlapping stream xoroshiro128 from globalOutlet. This generator has only Float64 and Uint64 prn methods.

func (*Xoro) Float64

func (x *Xoro) Float64() float64

Float64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution is 2^53 evenly spaced floats with spacing 2^-53.

func (*Xoro) Float64Bisect

func (x *Xoro) Float64Bisect(round bool) float64

Float64Bisect returns a uniformly distributed pseudo-random float64 value in [0, 1). If round is true, rounding is applied and the range is [0, 1]. All floats, normal and subnormal, are included.

func (*Xoro) Float64_64

func (x *Xoro) Float64_64() float64

Float64_64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-12, 1) and 2^52 evenly spaced floats in [0, 2^-12) with spacing 2^-64.

func (*Xoro) Float64_64R

func (x *Xoro) Float64_64R() float64

Float64_64R returns a uniformly distributed pseudo-random float64 from [0, 1] using rounding. The distribution includes all rounded floats in [2^-11, 1] and 2^53 evenly spaced floats in [0, 2^-11) with spacing 2^-64.

func (*Xoro) Float64_117

func (x *Xoro) Float64_117() float64

Float64_117 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-65, 1) and 2^52 evenly spaced floats in [0, 2^-65) with spacing 2^-117.

func (*Xoro) Float64_117R

func (x *Xoro) Float64_117R() float64

Float64_117R returns a uniformly distributed pseudo-random float64 from [0, 1] using rounding. The distribution includes all floats in [2^-65, 1] and 2^52 evenly spaced floats in [0, 2^-65) with spacing 2^-117.

func (*Xoro) Float64_128

func (x *Xoro) Float64_128() float64

Float64_128 --

func (*Xoro) Float64full

func (x *Xoro) Float64full() float64

Float64full returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [0, 1). Float64full is equivalent to Float64Bisect in truncate mode.

func (*Xoro) Float64fullR

func (x *Xoro) Float64fullR() float64

Float64fullR returns a uniformly distributed pseudo-random float64 from [0, 1] using rounding. The distribution includes all floats in [0, 1]. Float64fullR is equivalent to Float64Bisect in rounding mode.

func (*Xoro) Jump

func (x *Xoro) Jump()

Jump sets x to the same state as 2^64 calls to x.Uint64 or 2^32 calls to x.JumpShort.

func (*Xoro) JumpLong

func (x *Xoro) JumpLong()

JumpLong sets x to the same state as 2^96 calls to x.Uint64 or 2^32 calls to x.Jump.

func (*Xoro) JumpShort

func (x *Xoro) JumpShort()

JumpShort sets x to the same state as 2^32 calls to x.Uint64.

func (Xoro) NextState

func (x Xoro) NextState() Xoro

NextState returns the next Xoro state of the xoroshiro128+/** linear engine.

func (*Xoro) RandomReal

func (x *Xoro) RandomReal() float64

RandomReal returns a uniformly distributed pseudo-random float64 from [0, 1]. The distribution includes all floats, but may miss very few subnormal floats in in [0, 2^-1022). http://prng.di.unimi.it/random_real.c RandomReal is equivalent to Float64Bisect in rounding mode in [2^-1024, 1].

func (*Xoro) ReadState

func (x *Xoro) ReadState(b []byte)

ReadState reads the state of the generator x from b []byte.

func (*Xoro) Seed

func (x *Xoro) Seed(seed uint64)

Seed seeds a xorohiro128 generator by seed using splitMix64. Any seed is ok.

func (*Xoro) State

func (x *Xoro) State() []byte

State returns the current state of the generator x as []byte.

func (*Xoro) Uint64

func (x *Xoro) Uint64() (next uint64)

Uint64 returns a pseudo-random uint64. Uint64 is xoroshiro128**.

func (*Xoro) WriteState

func (x *Xoro) WriteState(b []byte)

WriteState writes the current state of the generator x to b. WriteState without allocation is faster than State().

func (*Xoro) Xoroshiro128plus

func (x *Xoro) Xoroshiro128plus() (next uint64)

Xoroshiro128plus is xoroshiro128+

type Xosh

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

A Xosh with a xoshiro256 prng implements a 64-bit generator with 256-bit state.

func NewXosh

func NewXosh(seed uint64) Xosh

NewXosh returns a new xoshiro256 generator seeded by the seed.

func NewXoshSlice

func NewXoshSlice(n int, seed uint64) []Xosh

NewXoshSlice returns a slice of n xoshiro256 generators with non-overlapping 2^128 long random streams. First generator is seeded by the seed.

func NextXosh

func NextXosh() Xosh

NextXosh returns the next non-overlapping stream xoshiro256 from globalOutlet. This generator has only Float64 and Uint64 prn methods.

func (*Xosh) Float64

func (x *Xosh) Float64() float64

Float64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes 2^53 evenly spaced floats with spacing 2^-53.

func (*Xosh) Float64_64

func (x *Xosh) Float64_64() float64

Float64_64 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-12, 1) and 2^52 evenly spaced floats in [0, 2^-12) with spacing 2^-64.

func (*Xosh) Float64_117

func (x *Xosh) Float64_117() float64

Float64_117 returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [2^-65, 1) and 2^52 evenly spaced floats in [0, 2^-65) with spacing 2^-117.

func (*Xosh) Float64full

func (x *Xosh) Float64full() float64

Float64full returns a uniformly distributed pseudo-random float64 from [0, 1). The distribution includes all floats in [0, 1). Float64full is equivalent to Float64Bisect in truncate mode.

func (*Xosh) Jump

func (x *Xosh) Jump()

Jump sets x to the same state as 2^128 calls to x.Uint64

func (*Xosh) JumpLong

func (x *Xosh) JumpLong()

JumpLong sets x to the same state as 2^192 calls to x.Uint64 or 2^64 calls to x.Jump.

func (Xosh) NextState

func (x Xosh) NextState() Xosh

NextState returns the next Xosh state of the xoshiro256 linear engine.

func (*Xosh) ReadState

func (x *Xosh) ReadState(b []byte)

ReadState reads the state of the generator x from b []byte.

func (*Xosh) Seed

func (x *Xosh) Seed(seed uint64)

Seed seeds a xoshiro256 by the seed using splitMix64. Any seed is ok.

func (*Xosh) State

func (x *Xosh) State() []byte

State returns the current binary state of the generator x as []byte.

func (*Xosh) Uint64

func (x *Xosh) Uint64() (next uint64)

Uint64 returns a pseudo-random uint64. Uint64 is xoshiro256**.

func (*Xosh) WriteState

func (x *Xosh) WriteState(b []byte)

WriteState writes the current state of the generator x to b. WriteState without allocations is faster than State().

func (*Xosh) Xoshiro256plus

func (x *Xosh) Xoshiro256plus() (next uint64)

Xoshiro256plus is xoshiro256+

func (*Xosh) Xoshiro256plusplus

func (x *Xosh) Xoshiro256plusplus() (next uint64)

Xoshiro256plusplus is xoshiro256++

Jump to

Keyboard shortcuts

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