How to generate a Bernuolli number

Hi, I want to randomly generate a Bernuolli number [0, 1],with probability p ~[0 1]. How to do that with boost? Thanks! -- ------------------------------- Enjoy life! Barco You

Barco You wrote:
I want to randomly generate a Bernuolli number [0, 1],with probability p ~[0 1]. How to do that with boost?
From reading the boost.random documentation for a minute, I came up with this: double p = ???; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
g(rng, d);
double x = g();

Hi Mathias, I did as you instructed. double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<
d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<> > g(rng, d);
double x = g();
But I got always 0 for x, why? ---- Best regards, Barco On Mon, Mar 30, 2009 at 9:46 PM, Mathias Gaunard < mathias.gaunard@ens-lyon.org> wrote:
Barco You wrote:
I want to randomly generate a Bernuolli number [0, 1],with probability p
~[0 1]. How to do that with boost?
From reading the boost.random documentation for a minute, I came up with this:
double p = ???;
boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<> > g(rng, d);
double x = g();
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

AMDG Barco You wrote:
Hi Mathias,
I did as you instructed.
double p = 0.5;
boost::mt19937 rng; boost::bernoulli_distribution<
d(p);
boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<> > g(rng, d);
double x = g();
But I got always 0 for x, why?
Did you try calling g() multiple times? The default constructor of boost::mt19937 puts it in a fixed state, so the sequence of numbers which it generates will always be the same. In Christ, Steven Watanabe

Hi Steven, Thanks for you reply! I call it 1000 time, and all output is 0. On Tue, Mar 31, 2009 at 9:52 AM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Barco You wrote:
Hi Mathias,
I did as you instructed.
double p = 0.5;
boost::mt19937 rng; boost::bernoulli_distribution<
d(p);
boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<> > g(rng, d);
double x = g();
But I got always 0 for x, why?
Did you try calling g() multiple times? The default constructor of boost::mt19937 puts it in a fixed state, so the sequence of numbers which it generates will always be the same.
In Christ, Steven Watanabe
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

AMDG Barco You wrote:
I call it 1000 time, and all output is 0.
Works for me... #include <boost/random.hpp> #include <iostream> int main() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<> > g(rng, d); for(int i = 0; i < 10; ++i) { std::cout << g() << std::endl; } } In Christ, Steven Watanabe

Hi Steven, Fine now! Thanks a lot! On Tue, Mar 31, 2009 at 11:11 AM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Barco You wrote:
I call it 1000 time, and all output is 0.
Works for me...
#include <boost/random.hpp> #include <iostream>
int main() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
g(rng, d);
for(int i = 0; i < 10; ++i) { std::cout << g() << std::endl;
} }
In Christ, Steven Watanabe
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

Hi, Sorry, wait!!! int main() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
g(rng, d);
for(int i = 0; i < 10; ++i) { std::cout << g() << std::endl; } } This really works. but If I write this in a function: int genB() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
g(rng, d);
return g() ; } int main(){ for(int i = 0; i < 10; ++i) { std::cout << genB() << std::endl; } } I'll got always 0.... Why? On Tue, Mar 31, 2009 at 2:06 PM, Barco You <barcojie@gmail.com> wrote:
Hi Steven,
Fine now! Thanks a lot!
On Tue, Mar 31, 2009 at 11:11 AM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Barco You wrote:
I call it 1000 time, and all output is 0.
Works for me...
#include <boost/random.hpp> #include <iostream>
int main() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
g(rng, d);
for(int i = 0; i < 10; ++i) { std::cout << g() << std::endl;
} }
In Christ, Steven Watanabe
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You
-- ------------------------------- Enjoy life! Barco You

Quoting Barco You <barcojie@gmail.com>:
Hi,
Sorry, wait!!! [...]
In the first case one variate_generator is constructed and it is used 10 times. In the second case 10 variate_generators are contructed and each used once. Since they are default constructed, they are each primed to return the same sequence. In particular they return the same first number, 0. Pete

Hi peter, Okay, I see now ,thanks! On Tue, Mar 31, 2009 at 4:03 PM, Peter Bartlett <pete@pcbartlett.com> wrote:
Quoting Barco You <barcojie@gmail.com>:
Hi,
Sorry, wait!!! [...]
In the first case one variate_generator is constructed and it is used 10 times.
In the second case 10 variate_generators are contructed and each used once. Since they are default constructed, they are each primed to return the same sequence. In particular they return the same first number, 0.
Pete
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

Hi, Continue this question. I want to have dynamic Bernoulli experiment, which has the p changed dynamically with time! How can I implement it? thanks! On Tue, Mar 31, 2009 at 4:12 PM, Barco You <barcojie@gmail.com> wrote:
Hi peter,
Okay, I see now ,thanks!
On Tue, Mar 31, 2009 at 4:03 PM, Peter Bartlett <pete@pcbartlett.com>wrote:
Quoting Barco You <barcojie@gmail.com>:
Hi,
Sorry, wait!!! [...]
In the first case one variate_generator is constructed and it is used 10 times.
In the second case 10 variate_generators are contructed and each used once. Since they are default constructed, they are each primed to return the same sequence. In particular they return the same first number, 0.
Pete
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You
-- ------------------------------- Enjoy life! Barco You

Hi, sorry about my first post. I didn't realize your question. About your problem: just reuse the number generator and construct a new instance of bernoulli_distribution with the new probability: int main() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p); typedef boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
generator;
generator g(rng, d); int time_steps = 2; for(int i = 0; i < 10; ++i) { // change probability every second step if(i % time_step == 0){ // change probability by 0.1 every second step p += 0.1; g = generator(rng, boost::bernoulli_distribution<>(p)); } std::cout << g() << std::endl; } } This does not work in the case of the number generator because it is declared as reference parameter. Henning Barco You schrieb:
Hi,
Continue this question. I want to have dynamic Bernoulli experiment, which has the p changed dynamically with time!
How can I implement it? thanks!
On Tue, Mar 31, 2009 at 4:12 PM, Barco You <barcojie@gmail.com> wrote:
Hi peter,
Okay, I see now ,thanks!
On Tue, Mar 31, 2009 at 4:03 PM, Peter Bartlett <pete@pcbartlett.com>wrote:
Quoting Barco You <barcojie@gmail.com>:
Hi,
Sorry, wait!!! [...]
In the first case one variate_generator is constructed and it is used 10 times.
In the second case 10 variate_generators are contructed and each used once. Since they are default constructed, they are each primed to return the same sequence. In particular they return the same first number, 0.
Pete
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

Hi Henning, Thanks for your reply! but, g = generator(rng, boost::bernoulli_distribution<>(p)); can not be compiled successfully, how to do? thanks! On Tue, Mar 31, 2009 at 6:58 PM, Henning Basold <h.basold@googlemail.com>wrote:
Hi, sorry about my first post. I didn't realize your question.
About your problem: just reuse the number generator and construct a new instance of bernoulli_distribution with the new probability:
int main() { double p = 0.5; boost::mt19937 rng; boost::bernoulli_distribution<> d(p);
typedef boost::variate_generator< boost::mt19937&, boost::bernoulli_distribution<>
generator;
generator g(rng, d); int time_steps = 2;
for(int i = 0; i < 10; ++i) { // change probability every second step if(i % time_step == 0){ // change probability by 0.1 every second step p += 0.1; g = generator(rng, boost::bernoulli_distribution<>(p)); } std::cout << g() << std::endl; } }
This does not work in the case of the number generator because it is declared as reference parameter.
Henning
Barco You schrieb:
Hi,
Continue this question. I want to have dynamic Bernoulli experiment, which has the p changed dynamically with time!
How can I implement it? thanks!
On Tue, Mar 31, 2009 at 4:12 PM, Barco You <barcojie@gmail.com> wrote:
Hi peter,
Okay, I see now ,thanks!
On Tue, Mar 31, 2009 at 4:03 PM, Peter Bartlett <pete@pcbartlett.com
wrote:
Quoting Barco You <barcojie@gmail.com>:
Hi,
Sorry, wait!!! [...]
In the first case one variate_generator is constructed and it is used 10 times.
In the second case 10 variate_generators are contructed and each used once. Since they are default constructed, they are each primed to return the same sequence. In particular they return the same first number, 0.
Pete
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

AMDG Barco You wrote:
Hi Henning,
Thanks for your reply! but, g = generator(rng, boost::bernoulli_distribution<>(p));
Try g.distribution() = boost::bernoulli_distribution<>(p); or just construct the variate generator whenever you need a random number: std::cout << generator(rng, boost::bernoulli_distribution<>(p))() << std::endl; In Christ, Steven Watanabe

Hi Steven, This way: g.distribution() = boost::bernoulli_distribution<>(p); works successfully!!! Many thanks! On Wed, Apr 1, 2009 at 9:09 AM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Barco You wrote:
Hi Henning,
Thanks for your reply! but, g = generator(rng, boost::bernoulli_distribution<>(p));
Try g.distribution() = boost::bernoulli_distribution<>(p); or just construct the variate generator whenever you need a random number: std::cout << generator(rng, boost::bernoulli_distribution<>(p))() << std::endl;
In Christ, Steven Watanabe
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You

Yes, my function just want to return 0 or 1, so I have the return type as int, is there any problem for that? On Tue, Mar 31, 2009 at 4:58 PM, Yaser Zhian <yaserzt@yahoo.com> wrote:
Barco You wrote:
This really works. but If I write this in a function: int genB() { ... }
Is your function really supposed to return an "int"?
-yzt _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- ------------------------------- Enjoy life! Barco You
participants (6)
-
Barco You
-
Henning Basold
-
Mathias Gaunard
-
Peter Bartlett
-
Steven Watanabe
-
Yaser Zhian