class Chi2 implements IContinuousDistribution {

    @Override
    /**
     * REFERENCE : - J.F. Monahan (1987): An algorithm for * generating chi
     * random variables, ACM Trans. * Math. Software 13, 168-172. 
     * Based on "cern.jet.random.ChiSquare", from the Colt library
     * Copyright (c) 1999 CERN - European Organization for Nuclear Research.
     */
    public double random(IRandomNumberGenerator rng) {

        if (df_ == 1) {
            while (true) {
                double u = rng.nextDouble();
                double v = rng.nextDouble() * 0.857763884960707;
                double z = v / u;
                if (z < 0) {
                    continue;
                }
                double zz = z * z;
                double r = 2.5 - zz;
                if (z < 0.0) {
                    r = r + zz * z / (3.0 * z);
                }
                if (u < r * 0.3894003915) {
                    return (z * z);
                }
                if (zz > (1.036961043 / u + 1.4)) {
                    continue;
                }
                if (2.0 * Math.log(u) < (-zz * 0.5)) {
                    return (z * z);
                }
            }
        } else {
            while (true) {
                double u = rng.nextDouble();
                double v = rng.nextDouble() * vd + vm;
                double z = v / u;
                if (z < -b) {
                    continue;
                }
                double zz = z * z;
                double r = 2.5 - zz;
                if (z < 0.0) {
                    r = r + zz * z / (3.0 * (z + b));
                }
                if (u < r * 0.3894003915) {
                    return ((z + b) * (z + b));
                }
                if (zz > (1.036961043 / u + 1.4)) {
                    continue;
                }
                if (2.0 * Math.log(u) < (Math.log(1.0 + z / b) * b * b - zz * 0.5 - z * b)) {
                    return ((z + b) * (z + b));
                }
            }
        }
    }

    @Deprecated public  Chi2();
    public  Chi2(final int df);

}