tr1 random variate generation queries

L

Lionel B

Greetings,

I am trying to get to grips with the tr1 random number generation system.
My first task is to generate real-valued uniform variates on a given
range. So the following produces expected results:

#include <iostream>
#include <tr1/random>

int main()
{
typedef std::tr1::mt19937 generator_t;
typedef std::tr1::uniform_real<double> distribution_t;
typedef std::tr1::variate_generator<generator_t, distribution_t> variate_t;

variate_t r(generator_t(), distribution_t(1.0, 2.0));

for (int i=0; i<10; ++i) std::cout << r() << std::endl;
}

Now I would like to try the same, but without using the `variate_generator'
template:

int main()
{
typedef std::tr1::mt19937 generator_t;
typedef std::tr1::uniform_real<double> distribution_t;

generator_t gen;
distribution_t dist(1.0, 2.0);

for (int i=0; i<10; ++i) std::cout << dist(gen) << std::endl;
}

This, as expected, does *not* work, since `uniform_real' requires a
real-valued generator uniform on [0,1), which `mt19937' is not. So my
question is, how to construct a real-valued generator uniform on [0,1)
in tr1? Is this even possible? If not, it would seem that `uniform_real'
is only useful in conjunction with `variate_generator'.

On the other hand, the second version works perfectly well with
`uniform_int' rather than `uniform_real'. This seems all somewhat
inconsistent.

Another question I have is whether tr1 makes any guarantees as to the
precision of real-valued variates. So e.g. if my `double' type has 53
bit precision, will a tr1 double variate (as, say, for my first example
above) generate randomness uniformly with 53-bit precision?

Cheers,
 
M

Martin Eisenberg

Lionel said:
int main()
{
typedef std::tr1::mt19937 generator_t;
typedef std::tr1::uniform_real<double> distribution_t;

generator_t gen;
distribution_t dist(1.0, 2.0);

for (int i=0; i<10; ++i) std::cout << dist(gen) <<
std::endl;
}

This, as expected, does *not* work, since `uniform_real'
requires a real-valued generator uniform on [0,1), which
`mt19937' is not. So my question is, how to construct a
real-valued generator uniform on [0,1) in tr1? Is this even
possible?

Seems not -- referring to N1836.pdf which I think is the final draft
of TR1, I find that Boost.Random's uniform_01 class has been dropped
in standardization.
Another question I have is whether tr1 makes any guarantees as
to the precision of real-valued variates.

Yes and no... On the one hand, 5.1.5 defines mt19937 with a word
length of 32 bits, and by 5.1.3/10 this is also the number of
mantissa bits that uniform_real will obtain from variate_generator's
internal engine (per call) in your example. On the other hand, since
5.1.1 Table 17 requires amortized constant runtime of
uniform_real::eek:perator() the final results will be quantized to
2^(k*32) levels, and the number of engine calls k is going to be
small and most probably 1. In particular, if the range [min,max[
comprises more than 2^(k*32) double values then full coverage is
precluded.


Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top