goark/mt -- Mersenne Twister; Pseudo Random Number Generator, Implemented by Golang
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
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()
}
$ 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
This package is licensed under MIT license.