-
Want a half open interval
[0,1) instead of closed interval [0,1].
In the last "return()" in the function genrand(),
(double)y is divided by (unsigned long)0xffffffff,
i.e. 2^32-1. If you change this to 2^32,
the output is distributed in [0,1).
How to construct the real constant 2^32
depends on your system, but for example
double zzz = ((double)((unsigned long) 0x80000000))*2;
sets the real 2^32 into zzz.
We guess there is a smarter way: let us know.
A master course student Sin Tanaka at the electoric
engineering department of Keio University suggested
to obtain a real 2^{-32} as a real constant.
The corresponding code is included on the main page.
-
Need more than 32 bits accuracy.
Concatenate two successive 32-bit integers to get a 64-bit integer.
-
To use as a random source for encryption.
The sequence is, as it is, not cryptographically secure.
However, if you use an appropriate Secure Hashing Algorithm
(non-invertible function compressing several words into one word),
then you may use them.
To be precise, in the existing encryption methods with
a linear pseudorandom generator (like GFSR),
to replace the generator with the large period MT
is valid.
In this case, you may need more than 2^32-1 initial seeds.
The initializing routine sgenrand(seed) of MT
is a mere linear-congruential generator.
If you do not use sgenrand,
then the actual initial seed consist of the 19937 bits
= the 19968 bits of the array mt[0..623] minus
the lower 31 bits of mt[0].
Help: we are not experts in this area. Please let us know appropriate
references and knowledges on cryptographically secure generators.
-
Want a uniform discrete distribution among the integers [0..N-1],
e.g. a dice in the case of N=6.
If the application is not sensitive to the rounding off
error, then please multiply N and take the integer part (sufficient for
most applications).
In the very rare case that the rounding off error of real numbers
does matter,
then generate random integers,
take the minimum integer with N<=2^n,
take the least significant n bits
and discard the numbers
greater than or equal to N.
A seed of 32-bit is too small. We want
a larger space for initial seeds.
Seed space of 622 words of 32-bit integers is available.
For example, in mt19937.c, there is an array named
mt[N]. Read the code of sgenrand(),
then you will find that the seed word is passed to mt[0],
and then mt[1], mt[2], ... are filled by a random number generator.
You may put the value 0xffffffff
to mt[0], and use all the rest mt[1]...mt[N-1]
as 622 words' seed space. Every initial value is safe.
Some versions of mt implements the array in a converse way,
so carefully read the code if you use other versions.