Statalist The Stata Listserver


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

st: RE: Random number


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Random number
Date   Fri, 9 Mar 2007 10:24:40 -0000

If -Maxy- is ever lower than -Miny-, then this
should be fixed somehow, _not_ worked around, 
as it could cause many other problems too. 

The appropriate check is either

	assert Maxy > Miny 

or

	assert Maxy >= Miny 

depending on what is appropriate. 

Nick 
n.j.cox@durham.ac.uk 

Austin Nichols
 
> Nasreen Khan --
> Ulrich Kohler's suggestion
>  gen ryear = Miny+int(uniform()*(Maxy-Miny+1))
> and my suggestion
>  gen ryear=Miny+round(uniform()*(Maxy-Miny+1)-.5)
> are equivalent except in some cases where, contrary to your belief
> about the data, Miny>Maxy.  In these cases, they both fail to give the
> desired result.
> 
> Even better might be the more robust:
>  gen ryear = min(Miny,Maxy)+int(uniform()*(abs(Maxy-Miny)+1))
> for this reason:
>  clear
>  range Miny -10000 10000 20001
>  set seed 7777
>  gen Maxy=Miny+ int(uniform()*13)-3
>  set seed 12345
>  gen ryear1=Miny+round(uniform()*(Maxy-Miny+1)-.5)
>  set seed 12345
>  gen ryear2=Miny+int(uniform()*(Maxy-Miny+1))
>  set seed 12345
>  gen ryear3=min(Miny,Maxy)+int(uniform()*(abs(Maxy-Miny)+1))
>  gen dy=max(Max,Min)-min(Max,Min)+1
>  gen r1=(ryear1-min(Maxy,Miny))/abs(Maxy-Miny)
>  gen r2=(ryear2-min(Maxy,Miny))/abs(Maxy-Miny)
>  gen r3=(ryear3-min(Maxy,Miny))/abs(Maxy-Miny)
>  tab r1 if dy==3
>  tab r2 if dy==3
>  tab r3 if dy==3

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index