Re: OONSTD: random number generators

E. Robert Tisdale (edwin@netwood.net)
Fri, 05 Mar 1999 11:05:44 -0800

Todd Veldhuizen wrote:

> E. Robert Tisdale writes:
> > 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?

Yes. It is called precision portability.
The portable application programmer includes

typedef float my_float;

at the beginning of the application program
and uses my_float instead of float
the substitutes

typedef double my_float;

when the application program is ported to a platform
which doesn't provide sufficient precision
for floating-point type float.

> 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?

The Uniform distribution
isn't actually a continuous uniform distribution.
It is a discrete uniform distribution to some precision
with constant distance delta between points.
You can specify delta to be no less than 2^{-32}
so that you can use a single 32-bit IRNG invocation
regardless of floating-point precision.
You can use a 64-bit IRNG
which won't cost much more to generate
if you are using MT19937.
You can cache the previous 32-bit IRNG
and use it to fill in the least significant bits
of a double or long double RNG.

Good Luck, Bob Tisdale <edwin@netwood.net>