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

* F distribution random number generator (optional noncentral)

* Example: rndf 1000 4 15 3 [set obs 1000; 4=numerator df;
*                               15=denominator df; 3=noncentrality ]

program define rndf
    version 3.1
    set type double
    cap drop xf
    qui     {
        local cases `1'
        set obs `cases'
        mac shift
        local dfn `1'
        mac shift
        local dfd `1'
mac shift
local lambda `1'
if "`lambda'" != "" {
    if `lambda' < 0 {
        di in re "noncentrality must be non-negative"
        exit 125
    }
    local labl = "noncentral "
    local delta = sqrt(2 * `lambda' / `dfn')
}
else { local delta = 0 }
        tempvar ran1 ran2
        noi di in gr "( Generating " _c
        local i=1
        gen `ran1' = 0
        while `i'<=`dfn'  {
replace `ran1' = `ran1'+ (invnorm(uniform()) + `delta')^2
            local i=`i'+1
            noi di in gr "." _c
        }
        noi di in gr "|" _c
        local i=1
        gen `ran2'=0
        while `i'<=`dfd'  {
            replace `ran2' = `ran2'+ (invnorm(uniform()))^2
            local i=`i'+1
            noi di in gr "." _c
        }

        gen xf = (`ran1'/`dfn')/(`ran2'/`dfd')
        noi di in gr " )"
        noi di in bl "Variable " in ye "xf " in bl "created."
lab var xf "`labl'F random variable"
        set type float
    }
end