Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Random] Discrete distribution behavior
From: sguazt (marco.guazzone_at_[hidden])
Date: 2012-06-04 16:53:51


Dear Booster,

I have a problem in understanding how the
boost::random::discrete_distribution works.

As far as I know, given the following events: x1, x2 and x2 and
weights: 1, 4, and 5,
* the associated PDF of the discrete distribution would be:
\Pr{X=x1}=1/10
\Pr{X=x2}=4/10
\Pr{X=x3}=5/10

* and the CDF would be:
\Pr{X<=x1}=1/10
\Pr{X<=x2}=5/10
\Pr{X<=x3}=1

So, if my random generator generates the value 0.814724, the discrete
random variate would return the event x3 (if I'm not wrong).

Instead, it seems that boost::random::discrete_distribution does not
work in this way.

To test it I have implemented a small program (see attachment).
Moreover, I have also implemented a naive version of discrete
distribution (called my::discrete_distribution), which simply draws a
uniform [0,1] random number and select the first event whose CDF is
greater than or equal to the drawn value.
I have also added some debug prints in other boost classes (just to
get the value of the generated random numbers).

I run it in Linux with Boost devel version + GCC 4.6.3 (with flags:
-Wall -ansi -pedantic)

Here's the output:

Testing Boost...
[boost::random::mersenne_twister] 3499211612
[boost::random::mersenne_twister] 581869302
[boost::random::discrete_distribution] test=0.814724 - result=0
Random Event: 1
[boost::random::mersenne_twister] 3890346734
[boost::random::mersenne_twister] 3586334585
[boost::random::discrete_distribution] test=0.905792 - result=2
Random Event: 2
[boost::random::mersenne_twister] 545404204
[boost::random::mersenne_twister] 4161255391
[boost::random::discrete_distribution] test=0.126987 - result=2
Random Event: 2

Testing My own version...
[boost::random::mersenne_twister] 3499211612
[my::discrete_distribution::rand] RNG: 0.814724
Random Event: 2
[boost::random::mersenne_twister] 581869302
[my::discrete_distribution::rand] RNG: 0.135477
Random Event: 1
[boost::random::mersenne_twister] 3890346734
[my::discrete_distribution::rand] RNG: 0.905792
Random Event: 2

As you can see, the sequence of Random Event is different.
Looking at the output of my::distribution, it seems it is working OK.

Can you help me in understanding what is going on?

Thank you very much for the help!!

Best,

-- Marco




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net