randomkit

package
v0.0.0-...-0c56139 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: MIT, MIT Imports: 5 Imported by: 0

README

randomkit

generate random sequences like numpy ones

Build Status Code Coverage Go Report Card GoDoc

Example
	var (
		state       *RKState
		a, expected []float64
		tol         = 1e-8
		ok          bool
	)
	// np.random.seed(7)
	// np.random.sample(5)
	state = NewRandomkitSource(7)
	a = make([]float64, 5)
	for i := range a {
		a[i] = state.Float64()
	}

	expected = []float64{0.07630829, 0.77991879, 0.43840923, 0.72346518, 0.97798951}
	ok = true
	for i := range a {
		ok = ok && math.Abs(expected[i]-a[i]) < tol
	}
	if !ok {
		fmt.Printf("expected %g got %g", expected, a)
	}

	expected = []float64{0.07630829, 0.77991879, 0.43840923, 0.72346518, 0.97798951}
	ok = true
	for i := range a {
		ok = ok && math.Abs(expected[i]-a[i]) < tol
	}
	if !ok {
		fmt.Printf("expected %g got %g", expected, a)
	}

	// test normal dist
	// np.random.seed(7)
	// np.random.standard_normal(5)
	state.Seed(7)
	for i := range a {
		a[i] = state.NormFloat64()
	}
	expected = []float64{1.6905257, -0.46593737, 0.03282016, 0.40751628, -0.78892303}
	ok = true
	for i := range a {
		ok = ok && math.Abs(expected[i]-a[i]) < tol
	}
	if !ok {

		fmt.Printf("expected %g got %g", expected, a)

	}

	// duplicate state have same future
	stateCopy := state.Clone()
	if state.Uint64() != stateCopy.Uint64() {
		fmt.Println("clone failure")
	}
warning

RKState is a suitable Source for golang.org/x/exp/rand but using rand.Float64 don't provide numpy-like sequences because rand.Float64 operations differ form RKState.Float64

The only way to get numpy-like sequences is to use RKState.Float64 or RKState.NormFloat64 directly

Documentation

Overview

Package randomkit is a partial port of randomkit(https://github.com/numpy/numpy/blob/master/numpy/random/mtrand/randomkit.c) in go

Index

Examples

Constants

View Source
const (
	N         = 624
	M         = 397
	MatrixA   = 0x9908b0df
	UpperMask = 0x80000000
	LowerMask = 0x7fffffff
)
Thomas Wang 32 bits integer hash function
func rkHash(key uint64) uint64 {
	key += ^(key << 15)
	key ^= (key >> 10)
	key += (key << 3)
	key ^= (key >> 6)
	key += ^(key << 11)
	key ^= (key >> 16)
	return key
}
func rkDevfill(buffer interface{}, size int, strong bool) error {
	var rfile *os.File
	var err error
	if strong {
		rfile, err = os.Open("/dev/random")
	} else {
		rfile, err = os.Open("/dev/urandom")
	}
	if err != nil {
		return errNoDev
	}
	err = binary.Read(rfile, binary.LittleEndian, buffer)
	if err == nil {
		return nil
	}
	return errNoDev

}

func rkRandomseed(state *RKState) error {
	if rkDevfill(state.key, rkStateLen*4, false) == nil {
		// ensures non-zero key
		state.key[0] |= 0x80000000
		state.pos = rkStateLen
		state.gauss = 0
		state.hasGauss = false
		state.hasBinomial = false

		for i := range state.key {
			state.key[i] &= 0xffffffff
		}
		return nil

	}
	state.Seed(uint64(time.Now().UnixNano()))
	return errNoDev
}
Magic Mersenne Twister constants

Variables

This section is empty.

Functions

This section is empty.

Types

type RKMathRandSource

type RKMathRandSource struct{ RKState *RKState }

RKMathRandSource is a wrapper to adapt RKState to math/rand

func (*RKMathRandSource) Clone

func (state *RKMathRandSource) Clone() rand.Source

Clone allow duplicating source state

func (*RKMathRandSource) Int63

func (state *RKMathRandSource) Int63() int64

Int63 for math/rand source

func (*RKMathRandSource) Seed

func (state *RKMathRandSource) Seed(seed int64)

Seed proto for math/rand source

func (*RKMathRandSource) Uint64

func (state *RKMathRandSource) Uint64() uint64

Uint64 for math/rand source

type RKState

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

RKState is a random source based on numpy's randomkit it implements "golang.org/x/exp/rand".Source and can be used as "math/rand".Source via AsMathRandSource but RKState Float64 and NormFloat64 methods must be used directly to reproduce original randomkit numpy sequences.

Example
var (
	state       *RKState
	a, expected []float64
	tol         = 1e-8
	ok          bool
)
// np.random.seed(7)
// np.random.sample(5)
state = NewRandomkitSource(7)
a = make([]float64, 5)
for i := range a {
	a[i] = state.Float64()
}

expected = []float64{0.07630829, 0.77991879, 0.43840923, 0.72346518, 0.97798951}
ok = true
for i := range a {
	ok = ok && math.Abs(expected[i]-a[i]) < tol
}
if !ok {
	fmt.Printf("expected %g got %g", expected, a)
}

// test normal dist
// np.random.seed(7)
// np.random.standard_normal(5)
state.Seed(7)
for i := range a {
	a[i] = state.NormFloat64()
}
expected = []float64{1.6905257, -0.46593737, 0.03282016, 0.40751628, -0.78892303}
ok = true
for i := range a {
	ok = ok && math.Abs(expected[i]-a[i]) < tol
}
if !ok {

	fmt.Printf("expected %g got %g", expected, a)

}

// duplicate state have same future
stateCopy := state.Clone()
if state.Uint64() != stateCopy.Uint64() {
	fmt.Println("clone failure")
}
Output:

func NewRandomkitSource

func NewRandomkitSource(seed uint64) (state *RKState)

NewRandomkitSource create a new randomkit source

func (*RKState) AsMathRandSource

func (state *RKState) AsMathRandSource() *RKMathRandSource

AsMathRandSource return RKState as a source suitable for rand.New

func (*RKState) Clone

func (state *RKState) Clone() rand.Source

Clone clones the randomkit state

func (*RKState) Float64

func (state *RKState) Float64() float64

Float64 uniform generator

func (*RKState) Int63

func (state *RKState) Int63() int64

Int63 generator

func (*RKState) Intn

func (state *RKState) Intn(n int) int

Intn returns, as an int, a non-negative pseudo-random number in [0,n). It panics if n <= 0.

func (*RKState) NormFloat64

func (state *RKState) NormFloat64() float64

NormFloat64 normal generator

func (*RKState) Perm

func (state *RKState) Perm(n int) []int

func (*RKState) Seed

func (state *RKState) Seed(seed uint64)

Seed initializes state with Knuth's PRNG

func (*RKState) Shuffle

func (state *RKState) Shuffle(n int, swap func(i, j int))

func (*RKState) SourceClone

func (state *RKState) SourceClone() rand.Source

Clone clones the randomkit state

func (*RKState) Uint32

func (state *RKState) Uint32() uint32

Uint32 generator

Slightly optimised reference implementation of the Mersenne Twister
Note that regardless of the precision of long, only 32 bit random
integers are produced

func (*RKState) Uint64

func (state *RKState) Uint64() uint64

Uint64 Returns an unsigned 64 bit random integer.

func (*RKState) Uint64n

func (state *RKState) Uint64n(n uint64) uint64

Uint64n returns, as a uint64, a pseudo-random number in [0,n). It is guaranteed more uniform than taking a Source value mod n for any n that is not a power of 2.

func (*RKState) Uint64s

func (state *RKState) Uint64s(off, rng uint64, out []uint64)

Uint64s Fills an array with random uint64 between off and off + rng inclusive. The numbers wrap if rng is sufficiently large.

Jump to

Keyboard shortcuts

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