random.hpp - suggestion for utility function

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) I was wondering if there would be any reason not to provide a utility function which made it easier to write random number generators. For instance, the following: template<class Dist_T, class Rng_T> typename boost::variate_generator<Rng_T, Dist_T>::result_type generate_rand(Dist_T dist, Rng_T rng) { return boost::variate_generator<Rng_T, Dist_T>(rng, dist)(); } would allow us to rewrite our rand function as follows. unsigned int shorthand_boost_rand() { return generate_rand(boost::uniform_int<>(0, RAND_MAX), boost::mt19937()); } By the way, congratulations on an excellent product to the random.hpp team. -- Christopher Diggins http://www.cdiggins.com

you really want to make some of those local variables static or you will get decidedly unrandom behaviour :-) regards, /michael toksvig "christopher diggins" <cdiggins@videotron.ca> wrote in message news:001e01c55fd1$393e6080$2b792518@heronnest...
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)
I was wondering if there would be any reason not to provide a utility function which made it easier to write random number generators. For instance, the following:
template<class Dist_T, class Rng_T> typename boost::variate_generator<Rng_T, Dist_T>::result_type generate_rand(Dist_T dist, Rng_T rng) { return boost::variate_generator<Rng_T, Dist_T>(rng, dist)(); }
would allow us to rewrite our rand function as follows.
unsigned int shorthand_boost_rand() { return generate_rand(boost::uniform_int<>(0, RAND_MAX), boost::mt19937()); }
By the way, congratulations on an excellent product to the random.hpp team.
-- Christopher Diggins http://www.cdiggins.com _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

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); I find 'variate_generator' to be only usefull when you want to get functional object to pass to some other function. If you want to directly produce random number, it's not needed.
I was wondering if there would be any reason not to provide a utility function which made it easier to write random number generators. For instance, the following:
template<class Dist_T, class Rng_T> typename boost::variate_generator<Rng_T, Dist_T>::result_type generate_rand(Dist_T dist, Rng_T rng) { return boost::variate_generator<Rng_T, Dist_T>(rng, dist)(); }
would allow us to rewrite our rand function as follows.
unsigned int shorthand_boost_rand() { return generate_rand(boost::uniform_int<>(0, RAND_MAX), boost::mt19937()); }
Did you try the above code? I have some suspicions that you might copy the mt19937 instance, and IIRC it's not that small. On a related note, I've run boost::mt19937 e; return boost::uniform_int<>(0, 100)(e); under profiler, and the price for constructing the mt19937 object and getting the int is roughly the same. This means that creating mt19937 object for each random number will give 2x running time increase. And as mentioned by Michael, the numbers won't be very randon either. - Volodya

From: "Vladimir Prus" <ghost@cs.msu.su> To: <boost@lists.boost.org> 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
participants (3)
-
christopher diggins
-
michael toksvig
-
Vladimir Prus