Statalist The Stata Listserver


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

st: Random number


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   st: Random number
Date   Thu, 8 Mar 2007 17:23:15 -0500

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