Prev Next

C++ Marsaglia Zaman Tsang Uniform Random Number Generator

Syntax
include <mztuni.hpp>
s = mztuni_seed(seed)
u = mztuni()

Purpose
This C++ random number generator is portable; to be specific the same seed gives same results for different computers. In addition, it is easy to implement and thereby avoids having to link in a large random number library; see mztuni.cpp .

Reference
G. Marsaglia, A. Zaman, and W.W. Tsang, Toward a universal random number generator, Statistics & Probability Letters, Volume 8, pages 35-39, 1990.

Typo
The Fortran version in Table 2 of the reference above contains the following line of code:
 
     CD = 7654321./167777216.
Using this value in mztuni does not produce the values in Table 4 of the reference. Replacing the value above by the value below does produce the values in Table 4:
 
     CD = 7654321./16777216.

include
Because it can be used without the rest of mat2cpp, this routine is not in the mat2cpp namespace and has its own include file.

seed

seed >= 2
The argument seed has prototype
     size_t 
seed
and is used to seed the random number generator. The function call
     CALL RSTART(
IJKL)
in the reference above corresponds to
     
seed = (((L-1)*168 + K-1)*168 + J-1)*168 + I + 1
Note the restriction that I , J , K and L are between 1 and 168 and not all equal to 1; i.e.,
     3 <= 
seed <= 796594177

seed == 1
If the special case where seed is equal to one, the current time is used to choose the actual seed between three and 796594177. This actual seed is then used to seed the random number generator.

seed == 0
In the special case where seed is equal to zero, no action is taken. This case is useful for retrieving the previous seed used to seed the random number generator.

s
The return value s has prototype
     size_t 
s
It is the previous seed that was used to seed the random number generator. If seed > 0 , the s corresponds to this call to mztuni_seed.

u
The return value u has prototype
     double 
u
It is sample of a random variable that is uniformly distributed between the values zero and one.

Example
The file mztuni_ok.cpp contains an example and test of this routine. It returns true if it succeeds and false otherwise.

Source
The file mztuni.cpp contains the C++ source code that implements mztuni.
Input File: omh/mztuni.omh