 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Pseudorandom Number from Supplied Table of Probabilities (Equivalent of SAS Rand("Table") Function)

 From Maarten Buis To statalist@hsphsun2.harvard.edu Subject Re: st: Pseudorandom Number from Supplied Table of Probabilities (Equivalent of SAS Rand("Table") Function) Date Mon, 6 Aug 2012 15:34:58 +0200

```On Mon, Aug 6, 2012 at 2:51 PM, Mustafa Hirji wrote:
> I would like to select one number from a a pre-defined set, where the probability of selecting each number is known.

*----------- begin example -----------
clear
set obs 100

//P(x=1) = .5
//P(x=2) = .2
//P(x=3) = .3
gen u = runiform()
gen x = cond(u < .5, 1, ///
cond(u < .7, 2, 3))

//P(y=1) = P(y=2)=...=P(y=6) = 1/6
gen y = ceil(6*runiform())
*------------ end example ------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

For more see:

M.L. Buis (2007), "Stata tip 48: Discrete uses for uniform()", The
Stata Journal, 7(3), pp. 434-435.
<http://www.stata-journal.com/article.html?article=pr0032>

and

<http://blog.stata.com/2012/07/18/using-statas-random-number-generators-part-1/>

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```