Statalist The Stata Listserver


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

Re: st: Random number


From   "Nasreen Khan" <[email protected]>
To   <[email protected]>
Subject   Re: st: Random number
Date   Thu, 08 Mar 2007 13:49:19 -0700

Thanks this one has a better distribution of years equally among years.

Nancy

>>> "Austin Nichols" <[email protected]> 3/8/2007 1:38 pm >>>
On second thought, probably not.
My example code generates rndyear on the endpoints about half as often
as interior years. If you want rndyear to be randomly distributed as
an integer on the interval [Min,Max] then you can:

gen rndyear=Miny+round(uniform()*(Maxy-Miny+1)-.5)

as shown in this example:
 clear
 range id 1 200000 200000
 gen Minyear=1990+floor(10*uniform())
 gen Maxyear=Minyear+floor(10*uniform())
 gen rndyear=Miny+round(uniform()*(Maxy-Miny+1)-.5)
 gen dy=Max-Min+1
 gen ril=(rndy-Miny)/(Maxy-Miny)
 su ril
 tab ril if dy==10
 di "Percent should be close to 10%"
 gen ryear=Miny+round(uniform()*(Maxy-Miny))
 gen r2=(ry-Miny)/(Maxy-Miny)
 tab r2 if dy==10
 di "Endpoints p=" %3.2f 100/18 ", interior p=" %3.2f 100/9

On 3/8/07, Austin Nichols <[email protected]> wrote:
> Like so?
>
> clear
> range id 1 2000 2000
> gen Minyear=1990+floor(10*uniform())
> gen Maxyear=Minyear+floor(10*uniform())
> gen rndyear=Miny+round(uniform()*(Maxy-Miny))
> su
>
> or perhaps you mean
> gen rndyr=cond(uniform()>.5,Miny,Maxy)
> ?
>
> On 3/8/07, Nasreen Khan <[email protected]> wrote:
> > I want to assign a random number based on the values of two variables.
> > My data set looks like this.
> >
> > ID       Minyear                      Maxyear
> > Nancy     1993                        1994
> > Ramon    1996                        1999
> > Eva         1992                        1992
> >
> > I want to create a variable called randomyear such that the year is randomly assignment between minyear and maxyear value.
>
*
*   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/


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