Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Random number generation in stata


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Random number generation in stata
Date   Wed, 8 Nov 2006 12:35:58 -0000

For uniform random numbers in intervals other than 0 to 1, 
just apply the linear transformation desired. 

It seems that you just want integers. You are aware of -int()-. 
-ceil()- and -floor()- also work nicely. 

gen u15_34 = 15 + floor(20 * uniform()) 

I don't think there is a shortcut when you want a mix of 
ranges. At some point you have to spell out all the details
and you might as well do that directly in a series of -generate-
statements. 

Nick 
[email protected] 

Patrick Musonda
 
> I am trying to create a simulation program in stata. In this 
> simulation, I
> would like to generate random numbers arising in various 
> ranges, for example
> numbers between 0 to10, 0 to15, 0 to 20, 20 to 10 etc for 
> several variables
> e.g. x1, x2, x3, x4 etc. Using the uniform distribution random number
> generator I can generate random numbers 0 to 10, or 0 to 100 
> without any
> problem by doing something like:
> 
> gen x1=int(10*uniform())
> 
> How can I generate random numbers from other ranges, for 
> example 15 to 35
> and how can I do this for several variables at once for example 5
> observation with three variables as shown below for random 
> numbers 1 to 10,
> 8 to 15 and 10 to 25 respectively as shown below
> 
> ob    x1   x2     x3
> 1      3     12     21
> 2      9     8       18
> 3      0     10     11
> 4     10    10     25
> 5      4     14     19
 

*
*   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/



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