Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: simulating bernoulli random numbers

From   Joseph Coveney <>
To   Statalist <>
Subject   Re: st: simulating bernoulli random numbers
Date   Thu, 03 Jul 2003 10:52:32 +0900

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 

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 

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:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index