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