mt

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 3 Imported by: 0

README

goark/mt -- Mersenne Twister; Pseudo Random Number Generator, Implemented by Golang

check vulns lint status GitHub license GitHub release

This package is "Mersenne Twister" algorithm, implemented by pure Go.

  • required Go 1.22 or later
  • Compatible with math/rand/v2 standard package.
  • Concurrency-safe (if it uses mt.PRNG type)

Migrated repository to github.com/goark/mt

Usage

Import
import "github.com/goark/mt/v2"
Usage with math/rand/v2 Standard Package (not concurrency-safe)
package main

import (
    "fmt"
    "math/rand/v2"

    "github.com/goark/mt/v2/mt19937"
)

func main() {
    fmt.Println(rand.New(mt19937.New(19650218)).Uint64())
    //Output:
    //13735441942630277712
}
Usage of mt.PRNG type (concurrency-safe version)
package main

import (
    "fmt"

    "github.com/goark/mt/v2"
    "github.com/goark/mt/v2/mt19937"
)

func main() {
    fmt.Println(mt.New(mt19937.New(19650218)).Uint64())
    //Output:
    //13735441942630277712
}
Use io.Reader interface
package main

import (
    "encoding/binary"
    "fmt"
    "math/rand/v2"
    "sync"

    "github.com/goark/mt/v2"
    "github.com/goark/mt/v2/mt19937"
)

func main() {
    wg := sync.WaitGroup{}
    prng := mt.New(mt19937.New(rand.Int64()))
    for i := 0; i < 1000; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            r := prng.NewReader()
            buf := [8]byte{}
            for i := 0; i < 10000; i++ {
                ct, err := r.Read(buf[:])
                if err != nil {
                    return
                }
                fmt.Println(binary.LittleEndian.Uint64(buf[:ct]))
            }
        }()
    }
    wg.Wait()
}

Benchmark Test

$ go test -bench Random -benchmem ./benchmark
goos: linux
goarch: amd64
pkg: github.com/goark/mt/v2/benchmark
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkRandomPCG-12              	785103775	         2.031 ns/op	       0 B/op	       0 allocs/op
BenchmarkRandomMT19917-12          	338082381	         3.551 ns/op	       0 B/op	       0 allocs/op
BenchmarkRandomPCGRand-12          	359948874	         3.288 ns/op	       0 B/op	       0 allocs/op
BenchmarkRandomMT19917Rand-12      	325159622	         3.687 ns/op	       0 B/op	       0 allocs/op
BenchmarkRandomChaCha8Locked-12    	186311572	         6.443 ns/op	       0 B/op	       0 allocs/op
BenchmarkRandomMT19917Locked-12    	128465040	         9.346 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/goark/mt/v2/benchmark	10.408s

License

This package is licensed under MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PRNG

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

PRNG is class of pseudo random number generator.

func New

func New(s Source) *PRNG

New returns new PRNG instance

func (*PRNG) NewReader

func (prng *PRNG) NewReader() *Reader

NewReader returns new Reader instance.

func (*PRNG) Real

func (prng *PRNG) Real(mode int) (f float64)

Real generates a random number on [0,1)-real-interval if mode==1, on (0,1)-real-interval if mode==2, on [0,1]-real-interval others

func (*PRNG) SeedArray

func (prng *PRNG) SeedArray(seeds []uint64)

SeedArray initializes Source.mt with seeds array

func (*PRNG) Uint64

func (prng *PRNG) Uint64() (n uint64)

Uint64 generates a random number on [0, 2^64-1]-interval

type Reader

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

Reader is class of pseudo random number generator with io.Reader interface.

func (*Reader) Read

func (r *Reader) Read(buf []byte) (int, error)

Read reads bytes data from generator (compatible with io.ReadCloser interface)

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

Read reads bytes data from generator (compatible with io.ReadCloser interface)

type Source

type Source interface {
	rand.Source
	SeedArray([]uint64)
	Real(int) float64
}

Source represents a source of uniformly-distributed

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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