GithubHelp home page GithubHelp logo

seehuhn / mt19937 Goto Github PK

View Code? Open in Web Editor NEW
73.0 73.0 16.0 35 KB

An implementation of Takuji Nishimura's and Makoto Matsumoto's Mersenne Twister pseudo random number generator in Go.

License: GNU General Public License v3.0

Go 100.00%

mt19937's People

Contributors

seehuhn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mt19937's Issues

The usages in README.rst and example.go are not goroutine-safe

README.rst and example.go says

rng := rand.New(mt19937.New())
rng.Seed(time.Now().UnixNano())

But this usage is not safe for concurrent use by multiple goroutines.

In fact, the following code sometimes panics:

package main

import (
	"math/rand"
	"sync"
	"time"

	"github.com/seehuhn/mt19937"
)

func main() {
	rng := rand.New(mt19937.New())
	rng.Seed(time.Now().UnixNano())

	num := 1000000

	var wg sync.WaitGroup
	start := make(chan struct{})
	for i := 0; i < 2; i++ {
		wg.Add(1)
		go func() {
			<-start
			for i := 0; i < num; i++ {
				_ = rng.Int()
			}
			wg.Done()
		}()
	}
	close(start)
	wg.Wait()
}
❯ go run main.go
panic: runtime error: index out of range [312] with length 312

goroutine 5 [running]:
github.com/seehuhn/mt19937.(*MT19937).Int63(0xc00000c060, 0x465861f52497b634)
	/Users/skaji/src/github.com/seehuhn/mt19937/mt19937.go:156 +0x23b
math/rand.(*Rand).Int63(...)
	/Users/skaji/env/go/src/math/rand/rand.go:85
math/rand.(*Rand).Int(...)
	/Users/skaji/env/go/src/math/rand/rand.go:103
main.main.func1(0xc000072060, 0xf4240, 0xc000078090, 0xc00001a0e0)
	/Users/skaji/try/20191103/main.go:24 +0x55
created by main.main
	/Users/skaji/try/20191103/main.go:21 +0x221
exit status 2

In math/rand, sources are wrapped by lockedSource.
https://golang.org/src/math/rand/rand.go#L288
I think README.md and example.go should suggest such usage too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.