mt19937 Module

Mersenne-Twister pseudorandom number generator (PRNG), specifically MT19937

This module has a type, mt19937_type, which can be used to have multiple, independent PRNGs within the same program. There is a module-level instance that can be used via the public procedures sgrnd and grnd, which can be used to have a consistent, reproducible PRNG across a whole program.

The original documentation comment follows:

A C-program for MT19937: Real number version genrand() generates one pseudorandom real number (double) which is uniformly distributed on [0,1]-interval, for each call. sgenrand(seed) set initial values to the working area of 624 words. Before genrand(), sgenrand(seed) must be called once. (seed is any 32-bit integer except for 0). Integer generator is obtained by modifying two lines. Coded by Takuji Nishimura, considering the suggestions by Topher Cooper and Marc Rieffel in July-Aug. 1997.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When you use this, send an email to: matumoto@math.keio.ac.jp with an appropriate reference to your work.


Fortran translation by Hiroshi Takano. Jan. 13, 1999.

This program uses the following non-standard intrinsics. ishft(i,n): If n>0, shifts bits in i by n positions to left. If n<0, shifts bits in i by n positions to right. iand (i,j): Performs logical AND on corresponding bits of i and j. ior (i,j): Performs inclusive OR on corresponding bits of i and j. ieor (i,j): Performs exclusive OR on corresponding bits of i and j.


Fortran 95 translation and modularized to use in gyrokinetics project by R. Numata, June 2, 2010. * removed statement functions (TSHFTU,TSHFTS,TSHFTT,TSHFTL) * uses [0,1) interval * the above bit manipulation functions became standard. * definition of UMASK is corrected (the original value cannot be represented as a kind=4 integer.



Contents


Variables

Type Visibility Attributes Name Initial
integer, private, parameter :: default_seed = 4357
integer, private, parameter :: N_mt19937 = 624

state size

integer, private, parameter :: M_mt19937 = 397

state size

type(mt19937_type), private :: module_mt19937

Module-level instance


Derived Types

type, public ::  mt19937_type

Mersenne-Twister object. Allows multiple, independent instances to be used within a program

Components

Type Visibility Attributes Name Initial
integer, public :: mt(0:N_mt19937-1)

The array for the state vector

integer, public :: mti = N_mt19937+1

mti==N+1 means mt[N] is not initialized

Type-Bound Procedures

procedure , public , :: set_seed Subroutine
procedure , public , :: generate Function

Functions

public function grnd()

Generate a random number from the module-level mt19937_type instance

Arguments

None

Return Value real

private function generate(this)

Generate a random number from a mt19937_type

Arguments

Type IntentOptional Attributes Name
class(mt19937_type), intent(inout) :: this

Return Value real


Subroutines

public subroutine sgrnd(seed)

Set initial seed of module-level mt19937_type instance

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: seed

private subroutine set_seed(this, seed)

Set initial seed of a mt19937_type instance

Read more…

Arguments

Type IntentOptional Attributes Name
class(mt19937_type), intent(inout) :: this
integer, intent(in) :: seed