Mersenne Twister for Visual Basic .NET

Home (Get Mersenne Twister for Visual Basic .NET there.)

Overview

Mersenne Twister (MT) is a long period, fast, high quality pseudo-random number generator (PRNG). Its period is 219937-1 (approximately 106002). The Visual Basic .NET code (mt19937ar.vb) produces over 60 million pseudo-random integers per second on a 2.66 GHz Intel Core2 Duo processor. MT has a sound theoretical basis and has been tested thoroughly. It exhibits equidistribution in 623 dimensions. The original MT authors' site has details about the algorithm and MT for other programming languages.

mt19937ar.vb has functions to produce 32- and 64-bit integers, as well as 32- and 53-bit precision reals. In all, mt19937ar.vb provides fifteen equidistribution generator functions and three initializing methods. Saving a generator's state to a file and loading it later is simple.

mt19937ar.vb has been tested successfully against the original authors' mt19937ar.c code by generating 100 million integers and 100 million reals with both, and comparing the entire outputs.

mt19937ar.vb passes George Marsalia's Diehard tests.

mt19937ar.vb implements MT as a VB class. Code is included in a separate file to demonstrate use of the VB class in your program. Test code for the class is also included to duplicate the original authors' mt19937ar.out file.

mt19937ar.vb has not been compiled with VB2003. (If you compile it with VB2003, please contact me.) It will not compile with VB 6 or earlier.

This is version 2.3, dated 2008-12-02, of mt19937ar.vb.

VB files in this package

FILE                        CONTENTS
------------------------    ---------------------------------------------------
mt19937ar.vb                The only file necessary to use the PRNGs.  It
                            consists of one Visual Basic .NET PRNG class:
                            MTRandom; and some exception classes used by
                            MTRandom.

Main.vb                     The original MT authors' test duplicated in VB .NET;
                            it also calls the following modules:

MakeEntropyTestData.vb      Makes a file to test with John Walker's 'ent.exe'
                            entropy and chi-squared program available at
                            http://www.fourmilab.ch/random/

Demo.vb                     Demonstrates how to initialize and use the PRNGs

SimplePerformanceTest.vb    The simple performance test described in mt19937ar.vb

Excerpts* from file mt19937ar.vb

' mt19337ar.vb version 2.3 (for Visual Basic .NET), 2008-12-02:
'
' This file implements the Mersenne Twister (MT), MT19937ar,
' pseudo-random number generator (PRNG) as a Visual Basic .NET
' class.
'
' Add this file (only) to your VB project to use the Mersenne Twister.
'
' This version (2.3) is unchanged functionally from version 2.2.  Only comments
' were changed from "Visual Basic 2005" to "Visual Basic .NET", reflecting
' the fact that the code was and is compatible with VB 2008 .NET and VB 2005 .NET.
'

'/*
'   A C-program for MT19937, with initialization improved 2002/1/26.
'   Coded by Takuji Nishimura and Makoto Matsumoto.
'
'   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.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
'   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
'*/

'
'- FUNCTIONS AND PROCEDURES IMPLEMENTED:
'
'       Function                    Returns values in the range:
'       -------------------------   -------------------------------------------------------
'       genrand_int32()             [0, 4294967295]  (0 to 2^32-1)
'
'       genrand_int31()             [0, 2147483647]  (0 to 2^31-1)
'
'       genrand_real1()             [0.0, 1.0]   (both 0.0 and 1.0 included)
'
'       genrand_real2()             [0.0, 1.0) = [0.0, 0.9999999997672...]
'                                                (0.0 included, 1.0 excluded)
'
'       genrand_real3()             (0.0, 1.0) = [0.0000000001164..., 0.9999999998836...]
'                                                (both 0.0 and 1.0 excluded)
'
'       genrand_res53()             [0.0,~1.0] = [0.0, 1.00000000721774...]
'                                                (0.0 included, ~1.0 included)
'
'       The following ADDITIONAL functions
'       ARE NOT PRESENT IN THE ORIGINAL C CODE:
'
'       NOTE: the limits shown below, marked with (*), are valid if gap==5.0e-13
'
'       genrand_int32SignedInt()   [-2147483648, 2147483647]   (-2^31 to 2^31-1)
'
'       genrand_real2b()           [0.0, 1.0)=[0, 1-(2*gap)] =[0.0, 0.9999999999990] (*)
'                                             (0.0 included, 1.0 excluded)
'
'       genrand_real2c()           (0.0, 1.0]=[0+(2*gap),1.0]=[1.0e-12, 1.0] (*)
'                                             (0.0 excluded, 1.0 included)
'
'       genrand_real3b()           (0.0, 1.0)=[0+gap, 1-gap] =[5.0e-13, 0.9999999999995] (*)
'                                             (both 0.0 and 1.0 excluded)
'
'
'       (See the "Acknowledgements" section for the following functions)
'
'       genrand_real4b()           [-1.0,1.0]=[-1.0, 1.0]
'                                             (-1.0 included, 1.0 included)
'
'       genrand_real5b()           (-1.0,1.0)=[-1.0+(2*gap), 1.0-(2*gap)]=
'                                                    [-0.9999999999990,0.9999999999990] (*)
'                                             (-1.0 excluded, 1.0 excluded)
'
'       genrand_intMax(upper)           [0,upper] for upper < 2^32  (0 to 4294967295 but <= upper)
'
'       genrand_intRange(lower,upper)   [lower,upper] for 0 <= lower <= upper <= 2^32-1
'                                                   (0 <= lower <= upper <= 4294967295)
'
'       genrand_int64()                 [0,18446744073709551615]    (0 to 2^64-1)
'
'
'       Procedure                 Arguments
'       ------------------------  ---------------------------------------------------------
'       init_genrand(seed)        any seed included in [0,4294967295]
'       init_by_array(array)      array has elements of type UInteger;
'                                 the array must have at least one element
'
'       [Visual Basic .NET]
'       init_random(True|False)   True:  reseed VB Random from the system clock,
'                                        then reseed MTRandom from VB Random
'                                 False: use the next value from VB Random to
'                                        reseed MTRandom
'       saveState(fileName)       any valid file name
'       loadState(fileName)       name of any file saved earlier with saveState()
'

'- USAGE:
'_________________________________________________________________________________________
' In your Visual Basic .NET application:
'
'   1) Add this file to your Visual Basic .NET project.
'
'   2) Create one or more instances of the MTRandom class.  The pseudo-random sequence
'      for each instance is initialized based on the arguments.
'      Make certain each instance stays in scope for the duration of its use so
'      it won't be re-initialized inadvertently.  Five different methods of
'      initialization are:
'
'           a) Dim r As New MTRandom()
'                   Initialize with seed == 5489 (the original MT
'                   authors' default seed).
'
'           b) Dim r As New MTRandom(19456)
'                   Initialize with seed==19456.  Any unsigned integer seed
'                   in the range [0,4294967295] is acceptable.
'
'           c) Dim init() As UInteger = {&H123, &H234, &H345, &H456}
'              Dim r As New MTRandom(init)
'                   Initialize with an array of unsigned integers (as in the
'                   original MT authors' version).  The array must have
'                   at least one element. 624 elements are desirable.  More
'                   or fewer elements are acceptable.  The element values
'                   should be random.
'
'           d) Dim r As New MTRandom(True)
'                   Seed VB Random from the system clock, and then
'                   seed MTRandom with the next value from VB Random.
'                   True is required but ignored.
'
'           e) Dim r As New MTRandom("filename.ext")
'                   Initialize with a generator state saved earlier with
'                   MTRandom.saveState("filename.ext")
'
'   3) Call any of the genrand_X() functions listed above.  You can re-
'      initialize an MTRandom instance at any point by calling init_genrand(),
'      init_by_array() or init_random().  You can save or load the PRNG state
'      at any point by calling saveState() or loadState().
'
'   4) Catch exceptions as desired.  The MTRandom exception classes are at the
'      end of this file.
'
' To test genrand_int32(), genrand_real2(), init_genrand() and init_by_array()
' in the Visual Basic .NET version of MT:
'
'   1) Create a Visual Basic .NET console application Project consisting of files
'           mt19937ar.vb
'           Demo.vb
'           SimplePerformanceTest.vb
'           MakeEntropyTestData.vb
'           Main.vb
'
'   2) Build and run the project.
'
'   3) Compare the file "mt19937arVBTest.out" with the original MT authors'
'      test output file ("mt19937ar.out", found by following
'      links at http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html).  
'      They should be identical.

'- ON PERFORMANCE:
'
'  SimplePerformanceTest.vb test results for MTRandom (Visual Basic .NET)
'
'   On a Dell Precision 390 workstation with 2.66 GHz Intel Core2 Duo processor,
'   3.5 GB RAM, Visual Basic .NET compiler, and generating 100,000,000 random numbers
'   with each function (running the Release version at a Cmd.exe prompt):
'
'                                   Seconds     Nanoseconds     Calls Per
'   Function                        Run Time    Per Call        Second
'   --------------------------      --------    -----------     ----------
'   genrand_int32()                  1.656        16.56         60,390,000
'   genrand_real1()                  2.031        20.31         49,240,000
'   genrand_res53()                  4.250        42.50         23,530,000
'   genrand_intMax(1000)             2.343        23.43         42,680,000
'   genrand_intMax(&H5FFFFFFF)       3.359        33.59         29,770,000
'   genrand_intRange(10,20)          3.813        38.13         26,230,000
'   genrand_int64()                  3.516        35.16         28,440,000

*These excerpts are from the header comments in file mt19937ar.vb. Please see the entire file header comments for further explanation of its development.

Contact

E-mail:
Ron Charlton <charltoncr at comcast.net>
9002 Balcor Circle
Knoxville, TN 37923-2301 USA
Phone: 865-694-0800

Bug reports, improvements, corrections, complaints or kudos are welcomed.

Document last revised: 2009-06-09