|
Boost : |
From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-05-24 09:27:15
From: "Vladimir Prus" <ghost_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, May 24, 2005 2:28 AM
Subject: [boost] Re: random.hpp - suggestion for utility function
> christopher diggins wrote:
>
>> AFAICT the shortest way to emulate the std::rand() function using
>> boost/random.hpp would be as follows:
>>
>> unsigned int longhand_boost_rand() {
>> boost::mt19937 rng;
>> boost::uniform_int<> dist(0, RAND_MAX);
>> boost::variate_generator
>> <
>> boost::mt19937,
>> boost::uniform_int<>
>> > generator(rng, dist);
>> return generator();
>> }
>>
>> (apologies if I made any mistakes)
>
> You can make it a bit shorter, I believe:
>
> boost::mt19937 e;
> return boost::uniform_int<>(0, 100)(e);
[snip]
Thanks you Vladmir and Michael for responding. So the correct shortest way
would then have been simply to write:
unsigned int boost_rand() {
static boost::mt19937 e;
return boost::uniform_int<>(0, RAND_MAX)(e);
}
?
So the purpose of variate_generator only generates function objects? The
introductory example http://www.boost.org/libs/random/ gives the impression
that it is a neccessary step. I think the documentation then should be
updated to be less confusing.
-- Christopher Diggins
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk