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 ¶
- Constants
- type RKMathRandSource
- type RKState
- func (state *RKState) AsMathRandSource() *RKMathRandSource
- func (state *RKState) Clone() rand.Source
- func (state *RKState) Float64() float64
- func (state *RKState) Int63() int64
- func (state *RKState) Intn(n int) int
- func (state *RKState) NormFloat64() float64
- func (state *RKState) Perm(n int) []int
- func (state *RKState) Seed(seed uint64)
- func (state *RKState) Shuffle(n int, swap func(i, j int))
- func (state *RKState) SourceClone() rand.Source
- func (state *RKState) Uint32() uint32
- func (state *RKState) Uint64() uint64
- func (state *RKState) Uint64n(n uint64) uint64
- func (state *RKState) Uint64s(off, rng uint64, out []uint64)
Examples ¶
Constants ¶
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 ¶
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) Intn ¶
Intn returns, as an int, a non-negative pseudo-random number in [0,n). It panics if n <= 0.
func (*RKState) NormFloat64 ¶
NormFloat64 normal generator
func (*RKState) SourceClone ¶
Clone clones the randomkit state
func (*RKState) 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