FMS 2025.01-dev
Flexible Modeling System
Loading...
Searching...
No Matches
MersenneTwister_mod

Fortran-95 implementation of the Mersenne Twister 19937 algorithm. More...

Data Types

interface  new_randomnumbersequence
 
type  randomnumbersequence
 The type containing the state variable. More...
 

Functions/Subroutines

subroutine, public finalize_randomnumbersequence (twister)
 
integer function, public getrandomint (twister)
 Generate a random integer on the interval [0,0xffffffff].
 
integer function, public getrandompositiveint (twister)
 Generate a random integer on the interval [0,0x7fffffff] or [0,2**31]. Equivalent to genrand_int31 in the C code.
 
real(r8_kind) function, public getrandomreal (twister)
 Generate a random number on [0,1] Equivalent to genrand_real1 in the C code. The result is stored as double precision but has 32 bit resolution.
 
type(randomnumbersequence) function initialize_scalar (seed)
 
type(randomnumbersequence) function initialize_vector (seed)
 
integer function mixbits (u, v)
 
subroutine nextstate (twister)
 
elemental integer function temper (y)
 
integer function twist (u, v)
 

Variables

integer, parameter lmask = 2147483647
 least significant r bits (0x7fffffffUL)
 
integer, parameter m = 397
 
integer, parameter matrix_a = -1727483681
 constant vector a (0x9908b0dfUL)
 
integer, parameter tmaskb = -1658038656
 (0x9d2c5680UL)
 
integer, parameter tmaskc = -272236544
 (0xefc60000UL)
 
integer, parameter umask = -2147483648_i8_kind
 most significant w-r bits (0x80000000UL)
 

Detailed Description

Fortran-95 implementation of the Mersenne Twister 19937 algorithm.

Author
Robert Pincus

Users must declare one or more variables of type randomNumberSequence in the calling procedure which are then initialized using a required seed. If the variable is not initialized the random numbers will all be 0. For example: program testRandoms use RandomNumbers type(randomNumberSequence) :: randomNumbers integer :: i

randomNumbers = new_RandomNumberSequence(seed = 100) do i = 1, 10 print ('(f12.10, 2x)'), getRandomReal(randomNumbers) end do end program testRandoms

Fortran-95 implementation by Robert Pincus NOAA-CIRES Climate Diagnostics Center Boulder, CO 80305 email: Rober.nosp@m.t.Pi.nosp@m.ncus@.nosp@m.colo.nosp@m.rado..nosp@m.edu

This documentation in the original C program reads:

A C-program for MT19937, with initialization improved 2002/2/10. Coded by Takuji Nishimura and Makoto Matsumoto. This is a faster version by taking Shawn Cokus's optimization, Matthe Bellew's simplification, Isaku Wada's real version.

Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length).

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Any feedback is very welcome. http://www.math.keio.ac.jp/matumoto/emt.html email: matum.nosp@m.oto@.nosp@m.math..nosp@m.keio.nosp@m..ac.j.nosp@m.p


Data Type Documentation

◆ mersennetwister_mod::new_randomnumbersequence

interface mersennetwister_mod::new_randomnumbersequence

Definition at line 122 of file mersennetwister.F90.

Public Member Functions

type(randomnumbersequence) function initialize_scalar (seed)
 
type(randomnumbersequence) function initialize_vector (seed)
 

Member Function/Subroutine Documentation

◆ initialize_scalar()

type(randomnumbersequence) function initialize_scalar ( integer, intent(in)  seed)

Definition at line 189 of file mersennetwister.F90.

◆ initialize_vector()

type(randomnumbersequence) function initialize_vector ( integer, dimension(0:), intent(in)  seed)

Definition at line 207 of file mersennetwister.F90.

◆ mersennetwister_mod::randomnumbersequence

type mersennetwister_mod::randomnumbersequence

The type containing the state variable.

Definition at line 116 of file mersennetwister.F90.

Collaboration diagram for randomnumbersequence:
[legend]

Public Attributes

integer currentelement
 
integer, dimension(0:blocksize -1) state
 

Member Data Documentation

◆ currentelement

integer currentelement

Definition at line 117 of file mersennetwister.F90.

◆ state

integer, dimension(0:blocksize -1) state

Definition at line 118 of file mersennetwister.F90.

Function/Subroutine Documentation

◆ finalize_randomnumbersequence()

subroutine, public finalize_randomnumbersequence ( type(randomnumbersequence), intent(inout)  twister)

Definition at line 313 of file mersennetwister.F90.

◆ getrandomint()

integer function, public getrandomint ( type(randomnumbersequence), intent(inout)  twister)

Generate a random integer on the interval [0,0xffffffff].

Equivalent to genrand_int32 in the C code. Fortran doesn't have a type that's unsigned like C does, so this is integers in the range -2**31 - 2**31 All functions for getting random numbers call this one, then manipulate the result

Definition at line 270 of file mersennetwister.F90.

◆ getrandompositiveint()

integer function, public getrandompositiveint ( type(randomnumbersequence), intent(inout)  twister)

Generate a random integer on the interval [0,0x7fffffff] or [0,2**31]. Equivalent to genrand_int31 in the C code.

Definition at line 284 of file mersennetwister.F90.

◆ getrandomreal()

real(r8_kind) function, public getrandomreal ( type(randomnumbersequence), intent(inout)  twister)

Generate a random number on [0,1] Equivalent to genrand_real1 in the C code. The result is stored as double precision but has 32 bit resolution.

Definition at line 299 of file mersennetwister.F90.

◆ initialize_scalar()

type(randomnumbersequence) function initialize_scalar ( integer, intent(in)  seed)
private

Definition at line 189 of file mersennetwister.F90.

◆ initialize_vector()

type(randomnumbersequence) function initialize_vector ( integer, dimension(0:), intent(in)  seed)
private

Definition at line 207 of file mersennetwister.F90.

◆ mixbits()

integer function mixbits ( integer, intent(in)  u,
integer, intent(in)  v 
)
private

Definition at line 136 of file mersennetwister.F90.

◆ nextstate()

subroutine nextstate ( type(randomnumbersequence), intent(inout)  twister)
private

Definition at line 154 of file mersennetwister.F90.

◆ temper()

elemental integer function temper ( integer, intent(in)  y)
private

Definition at line 174 of file mersennetwister.F90.

◆ twist()

integer function twist ( integer, intent(in)  u,
integer, intent(in)  v 
)
private

Definition at line 143 of file mersennetwister.F90.

Variable Documentation

◆ lmask

integer, parameter lmask = 2147483647
private

least significant r bits (0x7fffffffUL)

Definition at line 103 of file mersennetwister.F90.

◆ m

integer, parameter m = 397
private

Definition at line 103 of file mersennetwister.F90.

◆ matrix_a

integer, parameter matrix_a = -1727483681
private

constant vector a (0x9908b0dfUL)

Definition at line 103 of file mersennetwister.F90.

◆ tmaskb

integer, parameter tmaskb = -1658038656
private

(0x9d2c5680UL)

Definition at line 109 of file mersennetwister.F90.

◆ tmaskc

integer, parameter tmaskc = -272236544
private

(0xefc60000UL)

Definition at line 109 of file mersennetwister.F90.

◆ umask

integer, parameter umask = -2147483648_i8_kind
private

most significant w-r bits (0x80000000UL)

Definition at line 103 of file mersennetwister.F90.