Re: OONSTD: random number generators

Todd Veldhuizen (tveldhui@extreme.indiana.edu)
Fri, 5 Mar 1999 12:57:52 -0500 (EST)

E. Robert Tisdale writes:
> > The default IRNG is Matsumoto and Nishimura's
> > "Mersenne Twister" MT19937,
> > which has a period of 2^19937-1 (i.e. it will never repeat).
>
> Are you sure that you've got that right?
> It seems to imply at least 19937 bits of state information.
>
> But isn't it expensive?
> How many rand(), random() or drand48() calls can execute
> in the time it takes for one MT19937 call?

MT19937 is the nearest I've come to seeing pure magic in
an algorithm. It really does have a period of 2^19937-1
(yes, it does have 19937 bits of state).
And it has been clocked at 4 times the speed of rand().
See their web page (http://www.math.keio.ac.jp/~matumoto/emt.html)
for details.

> A sequence of floating-point random numbers
> initialized with the same seed should be the same
> regardless of type.

Interesting, I hadn't thought of this. Is it so people
can recompile their code with a double instead of float
and get a similar result?

I wonder if there is some way this capability could be
provided without sacrificing speed for float users. Maybe
for that situation, people could generate all their RNGs
as e.g. long double, then convert to float or double.
It seems wrong to penalize all users because some of them
want this feature?

> It is customary to overload
>
> T Uniform<T, IRNG, stateTag>::operator ()(void) {
> return random(): }

Yes, this is a good idea.

> How do you know that type unsigned int is 32 bits?
> I suggest that you look at the proposed ANSI C standard C9X
> and choose something like uint32_t.

Hmmm, this is a good point... looks like uint32_t didn't make
it into the C++ standard. I can put in a check for 32 bittedness
and have the preprocessor chew an #error if uint < 32 bits.

Thanks for the good advice!
Todd