[random] getting a 50% probability generator
I am at the point in my application where I must introduce randomness to a sort routine. Basically when 2 elements are equal, I need to create a 50% probability that they will be swapped. What is the correct boost::random library resource to code this? I've attempted to use uniform_01, by creating an instance of it with this type: typedef boost::uniform_01<boost::minstd_rand, double> rand_gen_t; However, after getting it constructed as a class attribute, I find accessing it with: cout << (*pRand_gen++) << endl; It produces numbers like 1 134784888 134784288 2 134784845 134784224 134785176 5 0 134784376 134785328 0. What I want is a sequence like 0 1 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 that is sort of random.
On 10/5/07, Jeffrey Holle <jeffreyholle@bellsouth.net> wrote:
I am at the point in my application where I must introduce randomness to a sort routine. Basically when 2 elements are equal, I need to create a 50% probability that they will be swapped.
What is the correct boost::random library resource to code this?
I've attempted to use uniform_01, by creating an instance of it with this type: typedef boost::uniform_01<boost::minstd_rand, double> rand_gen_t;
However, after getting it constructed as a class attribute, I find accessing it with: cout << (*pRand_gen++) << endl;
Try: cout << (*pRand_gen++) % 2 << endl; Richard
It produces numbers like 1 134784888 134784288 2 134784845 134784224 134785176 5 0 134784376 134785328 0.
What I want is a sequence like 0 1 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 that is sort of random.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
A few things, First, try this when creating your uniform_01 object, typedef boost::uniform_01<boost::mt19937> random_real; <--you typedef'ed it here. random_real actual_generator; actual_generator() <----to output numbers between 0 and 1. Two, don't use uniform_01. The uniform_real class works much better and defaults to between 0 and 1. Three, it looks like you are wanting random ints 0 and 1, not random real numbers between 0 and 1. For this have a look at the uniform_int or uniform_smallint class. They are probably what you are looking for. Hope this helps. Brian On 10/4/07, Jeffrey Holle <jeffreyholle@bellsouth.net> wrote:
I am at the point in my application where I must introduce randomness to a sort routine. Basically when 2 elements are equal, I need to create a 50% probability that they will be swapped.
What is the correct boost::random library resource to code this?
I've attempted to use uniform_01, by creating an instance of it with this type: typedef boost::uniform_01<boost::minstd_rand, double> rand_gen_t;
However, after getting it constructed as a class attribute, I find accessing it with: cout << (*pRand_gen++) << endl;
It produces numbers like 1 134784888 134784288 2 134784845 134784224 134785176 5 0 134784376 134785328 0.
What I want is a sequence like 0 1 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 that is sort of random.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
"Brian Stadler" <bdotstadler@gmail.com> writes:
Two, don't use uniform_01. The uniform_real class works much better and defaults to between 0 and 1.
Er, if uniform_real is much better than, and a superset of, uniform_01, what's the point of uniform_01 (or why isn't it defined in terms of uniform_real)? Thanks, -MIles -- Occam's razor split hairs so well, I bought the whole argument!
Brian Stadler wrote:
A few things,
First, try this when creating your uniform_01 object,
typedef boost::uniform_01<boost::mt19937> random_real; <--you typedef'ed it here. random_real actual_generator; actual_generator() <----to output numbers between 0 and 1.
Two, don't use uniform_01. The uniform_real class works much better and defaults to between 0 and 1.
Three, it looks like you are wanting random ints 0 and 1, not random real numbers between 0 and 1. For this have a look at the uniform_int or uniform_smallint class. They are probably what you are looking for.
Hope this helps.
Brian Thanks for this information. It looks like I indeed need uniform_smallint, but I'm having trouble with it. This is what I'm trying to compile:
std::tr1::mt19937 rng; boost::uniform_smallint<int> six(1,6); boost::variate_generator<std::tr1::mt19937&, boost::uniform_smallint<int> > die(rng, six); int x = die(); Note this from a boost webpage, with slight modifications. The compiler error I'm getting is: ../test.cpp:15: error: no matching function for call to 'boost::variate_generator<std::tr1::mt19937&, boost::uniform_smallint<int> >::variate_generator()' $BOOST_ROOT/boost/random/variate_generator.hpp:97: note: candidates are: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = std::tr1::mt19937&, Distribution = boost::uniform_smallint<int>] Can somebody point out what my problem is?
Stupid mistake! I was erroneously making the variate_generator a class attribute held by value. I succeeded getting what I need by using a uniform_smallint<> six(1,100) and generating my sequence with (*pRand_gen)() % 2. Thanks allot for the help. Jeffrey Holle wrote:
Brian Stadler wrote:
A few things,
First, try this when creating your uniform_01 object,
typedef boost::uniform_01<boost::mt19937> random_real; <--you typedef'ed it here. random_real actual_generator; actual_generator() <----to output numbers between 0 and 1.
Two, don't use uniform_01. The uniform_real class works much better and defaults to between 0 and 1.
Three, it looks like you are wanting random ints 0 and 1, not random real numbers between 0 and 1. For this have a look at the uniform_int or uniform_smallint class. They are probably what you are looking for.
Hope this helps.
Brian Thanks for this information. It looks like I indeed need uniform_smallint, but I'm having trouble with it. This is what I'm trying to compile:
std::tr1::mt19937 rng; boost::uniform_smallint<int> six(1,6); boost::variate_generator<std::tr1::mt19937&, boost::uniform_smallint<int> > die(rng, six); int x = die();
Note this from a boost webpage, with slight modifications.
The compiler error I'm getting is:
../test.cpp:15: error: no matching function for call to 'boost::variate_generator<std::tr1::mt19937&, boost::uniform_smallint<int> >::variate_generator()' $BOOST_ROOT/boost/random/variate_generator.hpp:97: note: candidates are: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = std::tr1::mt19937&, Distribution = boost::uniform_smallint<int>]
Can somebody point out what my problem is?
participants (4)
-
Brian Stadler
-
Jeffrey Holle
-
Miles Bader
-
Richard Dingwall