Chi2 implements IContinuousDistribution { @Override 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  Chi2();  Chi2(final int df); int getDegreesofFreedom(); @Override double getDensity(final double x); @Override String getDescription(); @Override double getExpectation(); @Override double getLeftBound(); @Override double getProbability(final double x, final ProbabilityType pt); @Override double getProbabilityInverse(double p, final ProbabilityType pt); @Override double getRightBound(); @Override double getVariance(); @Override BoundaryType hasLeftBound(); @Override BoundaryType hasRightBound(); @Override boolean isSymmetrical(); @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. */ double random(IRandomNumberGenerator rng); @Deprecated void setDegreesofFreedom(final int df); @Override String toString(); static final String NAME; }
@Test @Ignore public void testRandoms() { double s = 0; int N = 10000000; long t0 = System.currentTimeMillis(); Chi2 chi2 = new Chi2(20); MersenneTwister rng = MersenneTwister.fromSystemNanoTime(); for (int i = 0; i < N; ++i) { s += chi2.random(rng); } long t1 = System.currentTimeMillis(); System.out.println("New"); System.out.println(t1 - t0); System.out.println(s / N); }