/** * Create a NaturalRanking with the given NaNStrategy and TiesStrategy. * * @param nanStrategy NaNStrategy to use * @param tiesStrategy TiesStrategy to use */ public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) { super(); this.nanStrategy = nanStrategy; this.tiesStrategy = tiesStrategy; randomData = new RandomDataImpl(); }
/** * Uses a 2-cycle permutation shuffle to generate a random permutation. * The shuffling process is described * <a href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html"> * here</a>. * @param n the population size. * @param k the number to choose. * @return the random permutation. */ public int[] nextPermutation(int n, int k) { if (k > n) { throw new IllegalArgumentException ("permutation k exceeds n"); } if (k == 0) { throw new IllegalArgumentException ("permutation k must be > 0"); } int[] index = getNatural(n); shuffle(index, n - k); int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = index[n - i - 1]; } return result; }
/** * Uses a 2-cycle permutation shuffle to randomly re-order the last elements * of list. * * @param list * list to be shuffled * @param end * element past which shuffling begins */ private void shuffle(int[] list, int end) { int target = 0; for (int i = list.length - 1; i >= end; i--) { if (i == 0) { target = 0; } else { target = nextInt(0, i); } int temp = list[target]; list[target] = list[i]; list[i] = temp; } }
final RandomGenerator generator = getRan(); final double logLambda = Math.log(lambda); final double logLambdaFactorial = MathUtils.factorialLog((int) lambda); final long y2 = lambdaFractional < Double.MIN_VALUE ? 0 : nextPoisson(lambdaFractional); final double delta = Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1)); final double halfDelta = delta / 2; double qa = 0; for (;;) { final double u = nextUniform(0.0, 1); if (u <= p1) { final double n = nextGaussian(0d, 1d); x = n * Math.sqrt(lambda + halfDelta) - 0.5d; if (x > delta || x < -lambda) { final double e = nextExponential(1d); v = -e - (n * n / 2) + c1; } else { break; } else { x = delta + (twolpd / delta) * nextExponential(1d); y = Math.ceil(x); v = -nextExponential(1d) - delta * (x + 1) / twolpd;
/** * Generate a random value from a Normal (a.k.a. Gaussian) distribution * with the given mean, <code>mu</code> and the given standard deviation, * <code>sigma</code>. * * @param mu the mean of the distribution * @param sigma the standard deviation of the distribution * @return the random Normal value */ public double nextGaussian(double mu, double sigma) { if (sigma <= 0) { throw new IllegalArgumentException("Gaussian std dev must be > 0"); } RandomGenerator rand = getRan(); return sigma * rand.nextGaussian() + mu; }
/** * Generate a random int value uniformly distributed between * <code>lower</code> and <code>upper</code>, inclusive. This algorithm * uses a secure random number generator. * * @param lower the lower bound. * @param upper the upper bound. * @return the random integer. */ public int nextSecureInt(int lower, int upper) { if (lower >= upper) { throw new IllegalArgumentException ("lower bound must be < upper bound"); } SecureRandom sec = getSecRan(); return lower + (int) (sec.nextDouble() * (upper - lower + 1)); }
int[] index = nextPermutation(len, k); Object[] result = new Object[k]; for (int i = 0; i < k; i++) {
/** * Generates a random value sampled from this distribution. * * @return random value * @since 2.2 * @throws MathException if an error occurs generating the random value */ @Override public double sample() throws MathException { return randomData.nextGaussian(mean, standardDeviation); }
/** * Generates a random value sampled from this distribution. * * <p><strong>Algorithm Description</strong>: Uses the <a * href="http://www.jesus.ox.ac.uk/~clifford/a5/chap1/node5.html"> Inversion * Method</a> to generate exponentially distributed random values from * uniform deviates. </p> * * @return random value * @since 2.2 * @throws MathException if an error occurs generating the random value */ @Override public double sample() throws MathException { return randomData.nextExponential(mean); }
/** * Generates a random value sampled from this distribution. * * <p><strong>Algorithm Description</strong>: * <ul><li> For small means, uses simulation of a Poisson process * using Uniform deviates, as described * <a href="http://irmi.epfl.ch/cmos/Pmmi/interactive/rng7.htm"> here.</a> * The Poisson process (and hence value returned) is bounded by 1000 * mean.</li>< * * <li> For large means, uses the rejection algorithm described in <br/> * Devroye, Luc. (1981).<i>The Computer Generation of Poisson Random Variables</i> * <strong>Computing</strong> vol. 26 pp. 197-207.</li></ul></p> * * @return random value * @since 2.2 * @throws MathException if an error occurs generating the random value */ @Override public int sample() throws MathException { return (int) FastMath.min(randomData.nextPoisson(mean), Integer.MAX_VALUE); }
final RandomGenerator generator = getRan(); final double logLambda = FastMath.log(lambda); final double logLambdaFactorial = MathUtils.factorialLog((int) lambda); final long y2 = lambdaFractional < Double.MIN_VALUE ? 0 : nextPoisson(lambdaFractional); final double delta = FastMath.sqrt(lambda * FastMath.log(32 * lambda / FastMath.PI + 1)); final double halfDelta = delta / 2; double qa = 0; for (;;) { final double u = nextUniform(0.0, 1); if (u <= p1) { final double n = nextGaussian(0d, 1d); x = n * FastMath.sqrt(lambda + halfDelta) - 0.5d; if (x > delta || x < -lambda) { final double e = nextExponential(1d); v = -e - (n * n / 2) + c1; } else { break; } else { x = delta + (twolpd / delta) * nextExponential(1d); y = FastMath.ceil(x); v = -nextExponential(1d) - delta * (x + 1) / twolpd;
/** * Generate a random int value uniformly distributed between * <code>lower</code> and <code>upper</code>, inclusive. * * @param lower the lower bound. * @param upper the upper bound. * @return the random integer. */ public int nextInt(int lower, int upper) { if (lower >= upper) { throw new IllegalArgumentException ("upper bound must be > lower bound"); } RandomGenerator rand = getRan(); double r = rand.nextDouble(); return (int)((r * upper) + ((1.0 - r) * lower) + r); }
/** * Generate a random long value uniformly distributed between * <code>lower</code> and <code>upper</code>, inclusive. This algorithm * uses a secure random number generator. * * @param lower the lower bound. * @param upper the upper bound. * @return the random integer. */ public long nextSecureLong(long lower, long upper) { if (lower >= upper) { throw new IllegalArgumentException ("lower bound must be < upper bound"); } SecureRandom sec = getSecRan(); return lower + (long) (sec.nextDouble() * (upper - lower + 1)); }
int[] index = nextPermutation(len, k); Object[] result = new Object[k]; for (int i = 0; i < k; i++) {
/** * Generate a random long value uniformly distributed between * <code>lower</code> and <code>upper</code>, inclusive. * * @param lower the lower bound. * @param upper the upper bound. * @return the random integer. */ public long nextLong(long lower, long upper) { if (lower >= upper) { throw new IllegalArgumentException ("upper bound must be > lower bound"); } RandomGenerator rand = getRan(); double r = rand.nextDouble(); return (long)((r * upper) + ((1.0 - r) * lower) + r); }
int[] index = getNatural(n); shuffle(index, n - k); int[] result = new int[k]; for (int i = 0; i < k; i++) {
/** * Create a NaturalRanking with the given TiesStrategy. * * @param tiesStrategy the TiesStrategy to use */ public NaturalRanking(TiesStrategy tiesStrategy) { super(); this.tiesStrategy = tiesStrategy; nanStrategy = DEFAULT_NAN_STRATEGY; randomData = new RandomDataImpl(); }
SecureRandom secRan = getSecRan(); MessageDigest alg = null; try {
/** * Uses a 2-cycle permutation shuffle to randomly re-order the last elements * of list. * * @param list list to be shuffled * @param end element past which shuffling begins */ private void shuffle(int[] list, int end) { int target = 0; for (int i = list.length - 1 ; i >= end; i--) { if (i == 0) { target = 0; } else { target = nextInt(0, i); } int temp = list[target]; list[target] = list[i]; list[i] = temp; } }
int[] index = nextPermutation(len, k); Object[] result = new Object[k]; for (int i = 0; i < k; i++) {