Thomas Mählmann asked about generating random numbers of a Bernoulli
distribution, and Roger Newson described the standard procedure that
utilizes the -uniform()- pseudorandom number generating function.
(Earlier, Nix Cox responded similarly to essentially the same
question from Feiyean.)
There is also a versatile suite of pseudorandom number generators
written by Joseph Hilbe and Walter Linde-Zwirble, with later help
from Thomas Steichen, for a variety of distributions (-findit rnd-).
I use the suite extensively in power estimations and other
simulations.
If you look at the code of the two functions for binomial random
variates (-rndbin- and -rndbinx-), it seems that the there are
several variations on the -generate byte randu = uniform() <= pi-
approach mentioned by Roger and Nick. The variations handle
situations involving small denominators (series of Bernoulli trials)
and small expected numbers (small pi). I recommend considering using
these commands in lieu of -generate byte randu = uniform() <= pi-,
especially if your problem might involve either of these special
circumstances.
Joseph Coveney
P.S. The suite was written for an earlier release of Stata, and so it
uses Stata's old pseudorandom number generating function (-set seed0-
), and other superceded Stata commands. In order to make it run more
quickly and in order to have it use the modern pseudorandom number
generator, I modified the code for the two commands that I use most
(for the binomial distribution, -rndbin- and -rndbinx-), placing the
two commands under a different name in my personal ado directory. It
might be worthwhile someday to go through the entire suite and update
the code . . .
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/