Code Monkey home page Code Monkey logo

mt19937's Introduction

The Mersenne Twister in Go

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

Copyright (C) 2013 Jochen Voss

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The homepage of this package is at <http://www.seehuhn.de/pages/mt19937>. Please send any comments or bug reports to the program's author, Jochen Voss <[email protected]>.

Overview

The Mersenne Twister is a pseudo random number generator (PRNG), developed by Takuji Nishimura and Makoto Matsumoto. The Mersenne Twister is, for example, commonly used in Monte Carlo simulations and is the default random number generator for many programming languages, e.g. Python and R. This package implements the 64bit version of the algorithm.

Installation

This package can be installed using the go get command:

go get github.com/seehuhn/mt19937

Usage

Detailed usage instructions are available via the package's online help, either on godoc.org or on the command line:

go doc github.com/seehuhn/mt19937

The class MT19937 represents instances of the Mersenne Twister. New instances can be allocated using the mt19937.New() function. A seed can be set using the .Seed() or .SeedFromSlice() methods. MT19937 implements the rand.Source interface from the math/rand package. Typically the PRNG is wrapped in a rand.Rand object as in the following example:

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

Comparison to the Go Default PRNG

Go has a built-in PRNG provided by the math/rand package. I did not find any information about this built-in PRNG except for a comment in the source code which says "algorithm by DP Mitchell and JA Reeds". In contrast, the MT19737 generator provided in this package is a well-understood random number generator. Relevant references include [Ni2000] and [MatNi1998].

[Ni2000]T. Nishimura, Tables of 64-bit Mersenne Twisters, ACM Transactions on Modeling and Computer Simulation 10, 2000, pages 348-357.
[MatNi1998]M. Matsumoto and T. Nishimura, Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator, ACM Transactions on Modeling and Computer Simulation 8, 1998, pages 3--30.

The unit tests for the mt19937 package verify that the output of the Go implementation coincides with the output of the references implementation.

The mt19937 generator is slightly slower than the Go default PRNG. A speed comparison can be performed using the following command:

go test -bench=. github.com/seehuhn/mt19937

On my (64bit) system I get the following results:

method time per call thoughput
MT19937.Uint64 14.9 ns/op 537.48 MB/s
MT19937.Int63 15.0 ns/op 533.54 MB/s
builtin Int63 11.9 ns/op 674.07 MB/s

This shows that, on my system, a call to the Int63() method of the built-in PRNG takes about 80% of the time that MT19937.Int63() takes.

mt19937's People

Contributors

seehuhn avatar

Watchers

James Cloos avatar Tibor Halter avatar

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.