Boost logo

Boost :

From: Phil Richards (news_at_[hidden])
Date: 2006-03-18 06:10:06


On 2006-03-17, John Maddock <john_at_[hidden]> wrote:
> I seriously hope I'm doing something wrong here, but the last 20 bits in a
> double returned from uniform_real appear to always be zero. Is this by
> design? I really hope not!

Take a look at the implementation of uniform_real - you'll see that it
does:

    template<class Engine>
    result_type operator()(Engine& eng) {
        return static_cast<result_type>(eng() - eng.min())
            / static_cast<result_type>(eng.max() - eng.min())
            * (_max - _min) + _min;
    }

(with suitable removal of min/max protection macros.)

Since the engine is a integer based generator (probably 32 bits on most
32 bit platforms) there is no way it can generate more than 32 bits of
randomness with this implementation. Hence the 20 bits of zeroes at the
end of the double.

What it really needs to do is detect that the engine type hasn't got enough
bits and call the engine repeatedly until it has filled the result_type
up...

phil

-- 
change name before "@" to "phil" for email

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