Boost logo

Boost :

Subject: Re: [boost] [Random]Random value from range
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-01-03 13:59:56


AMDG

Mariusz Kwiczala wrote:
> After experiments I wrote below function.
> Could you verify if function is written correctly for random value from
> range [min,max) and
> uniform distribution?
>
> double RandomGenerator(const double min, const double max)
> {
> assert(min<max);
> static boost::lagged_fibonacci44497 gen(static_cast<unsigned
> int>(std::time(0)));
> static boost::uniform_real<> range(min, max);
> static boost::variate_generator<boost::lagged_fibonacci44497&,
> boost::uniform_real<> > generator(gen, range);
> const double value = generator();
> return value;
> }
>
> I have few question related to random library:
> 1. Is it possibility to write function which will generate numbers
> from range [min, max] or (min, max)
>

(min, max) can be handled by rejection from [min, max).
[min, max] is a bit trickier. Either way, you'll have to implement
it yourself.

> 2. Why generator (boost::lagged_fibonacci44497)is not properly
> initialized in some default constructor. Without static_cast<unsigned
> int>(std::time(0)) code generated the same values after every application
> run.
>

This is a feature.

> 3. Why when I missed reference "&" in line
> boost::variate_generator<boost::lagged_fibonacci44497&, application crashes.
>

Probably stack overflow. boost::lagged_fibonacci44497 is
rather large.

> Is it bug?
>

In Christ,
Steven Watanabe


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