Boost logo

Boost :

Subject: [boost] [random] Quantization effects in generating floating point values
From: John Maddock (jz.maddock_at_[hidden])
Date: 2015-03-05 08:39:04


First off, I notice there are no examples for generating floating point
values in Boost.Random, so maybe what follows is based on a
misunderstanding, or maybe not...

Lets say I generate values in [0,1] like so:

    boost::random::mt19937 engine;
    boost::random::uniform_01<boost::random::mt19937, FPT> d(engine);

    FPT d = d(); //etc

Where FPT is some floating point type.

Now my concern is that we're taking a 32-bit random integer and
"stretching" it to a floating point type with rather more bits (53 for a
double, maybe 113 for a long double, even more in the multi-precision
world). So quantization effects will mean that there are many values
which can never be generated.

It's true that I could use independent_bits_engine to gang together
multiple random values and then pass that to uniform_01, however that
supposes we have an unsigned integer type available with enough bits.
cpp_int from boost.multiprecision would do it, and this does work, but
the conversions involved aren't particularly cheap. It occurs to me
that an equivalent to independent_bit_engine but for floating point
types could be much more efficient - especially in the binary floating
point case.

So I guess my questions are:

Am I worrying unnecessarily? and
What is best practice in this area anyway?

Thanks, John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk