mt19937

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2019 License: ISC Imports: 0 Imported by: 0

Documentation

Overview

Package mt19937 implements a 64-bit version of the Mersenne Twister pseudo-random number generator (MT19937 PRNG).

The state size is 312 uint64.

This is a pure Go implementation based on the mt19937-64.c C implementation by Makoto Matsumoto and Takuji Nishimura.

More information on the Mersenne Twister algorithm and other implementations are available from http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

See included LICENSE_MT for original C code and license.

Example
package main

import (
	"fmt"

	"github.com/db47h/rand64/v3/mt19937"
)

func main() {
	init := []uint64{
		0x12345, 0x23456, 0x34567, 0x45678,
	}
	mt := new(mt19937.Rng)
	mt.SeedFromSlice(init)

	fmt.Println("10 outputs of Rng.Uint64()")
	for i := 0; i < 10; i++ {
		fmt.Printf(" %20d", mt.Uint64())
		if i%5 == 4 {
			fmt.Println()
		}
	}
	for i := 0; i < 1000; i++ {
		mt.Uint64()
	}
	fmt.Println("10 more")
	for i := 0; i < 10; i++ {
		fmt.Printf(" %20d", mt.Uint64())
		if i%5 == 4 {
			fmt.Println()
		}
	}

}
Output:

10 outputs of Rng.Uint64()
  7266447313870364031  4946485549665804864 16945909448695747420 16394063075524226720  4873882236456199058
 14877448043947020171  6740343660852211943 13857871200353263164  5249110015610582907 10205081126064480383
10 more
 14907209235746902445 15452338815569321965 17045090235069538607 15507333859934612093   157175897107904252
  2578005313950236321  6502648805754593060 13133523174961431106  2698278206396822833  3278969850082110371

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rng

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

Rng wraps the state data for the MT19937 pseudo-random number generator.

func (*Rng) Int63

func (rng *Rng) Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64.

func (*Rng) Seed

func (rng *Rng) Seed(seed int64)

Seed uses the provided uint64 seed value to initialize the generator to a deterministic state.

If Seed is 0, the generator will be seeded with the same default value as in the original C code (5489).

func (*Rng) SeedFromSlice

func (rng *Rng) SeedFromSlice(key []uint64)

SeedFromSlice initializes the state array with data from slice key. This function behaves exactly like init_by_array() in the original C code.

func (*Rng) Uint64

func (rng *Rng) Uint64() uint64

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

Jump to

Keyboard shortcuts

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