*!version 1.1.1 2000 Joseph Hilbe   /Thomas Steichen added noncentrality feature 17May00
* version 1.0.0 1993 Joseph Hilbe                           (sg44: STB-28)

* Student's t distribution random number generator (optional noncentral)

* Example: rndt 1000 10 3 [set obs 1000;  10 is the degrees of freedom;
*                           3 is the noncentrality ]

program define rndt
    version 3.1
    set type double
    cap drop xt
    qui     {
        local cases `1'
        set obs `cases'
        mac shift
        local df `1'
mac shift
local delta `1'
if "`delta'" != "" { local labl = "noncentral " }
else { local delta = 0 }
        tempvar ran1 z
        noi di in gr "( Generating " _c
        local i=1
gen `z'=invnorm(uniform()) + `delta'
        gen `ran1'=0
        while `i'<=`df'  {
            replace `ran1' = `ran1'+ (invnorm(uniform()))^2
            local i=`i'+1
            noi di in gr "." _c
        }
        gen xt = `z'/sqrt(`ran1'/`df')
        noi di in gr " )"
        noi di in bl "Variable " in ye "xt " in bl "created."
lab var xt "`labl't random variable"
        set type float
    }
end