/** * 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. * * @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. * * @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); }
double r = 1.0d; double rnd = 1.0d; RandomGenerator rand = getRan(); while (n < 1000 * mean) { rnd = rand.nextDouble();
RandomGenerator ran = getRan();
/** * Returns a random value from an Exponential distribution with the given * mean. * <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> * * @param mean the mean of the distribution * @return the random Exponential value */ public double nextExponential(double mean) { if (mean < 0.0) { throw new IllegalArgumentException ("Exponential mean must be >= 0"); } RandomGenerator rand = getRan(); double unif = rand.nextDouble(); while (unif == 0.0d) { unif = rand.nextDouble(); } return -mean * Math.log(unif); }
/** * 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 * @throws NotStrictlyPositiveException if {@code sigma <= 0}. */ public double nextGaussian(double mu, double sigma) { if (sigma <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sigma); } return sigma * getRan().nextGaussian() + mu; }
RandomGenerator ran = getRan();
/** * 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. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public long nextLong(long lower, long upper) { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } double r = getRan().nextDouble(); return (long) ((r * upper) + ((1.0 - r) * lower) + r); }
/** * 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. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public int nextInt(int lower, int upper) { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } double r = getRan().nextDouble(); return (int) ((r * upper) + ((1.0 - r) * lower) + r); }
/** * {@inheritDoc}<p> * <strong>Algorithm Description</strong>: scales the output of * Random.nextDouble(), but rejects 0 values (i.e., will generate another * random double if Random.nextDouble() returns 0). * This is necessary to provide a symmetric output interval * (both endpoints excluded).</p> * * @param lower the lower bound. * @param upper the upper bound. * @return a uniformly distributed random value from the interval (lower, upper) */ public double nextUniform(double lower, double upper) { if (lower >= upper) { throw new IllegalArgumentException ("lower bound must be < upper bound"); } RandomGenerator rand = getRan(); // ensure nextDouble() isn't 0.0 double u = rand.nextDouble(); while(u <= 0.0){ u = rand.nextDouble(); } return lower + u * (upper - lower); }
/** * 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 MathRuntimeException.createIllegalArgumentException( "standard deviation must be positive ({0})", sigma); } return sigma * getRan().nextGaussian() + mu; }
/** * 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 MathRuntimeException.createIllegalArgumentException( "upper bound ({0}) must be greater than lower bound ({1})", upper, lower); } double r = getRan().nextDouble(); return (int) ((r * upper) + ((1.0 - r) * lower) + r); }
RandomGenerator ran = getRan();
/** * 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 MathRuntimeException.createIllegalArgumentException( "upper bound ({0}) must be greater than lower bound ({1})", upper, lower); } double r = getRan().nextDouble(); return (long) ((r * upper) + ((1.0 - r) * lower) + r); }
/** * Returns a random value from an Exponential distribution with the given * mean. * <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> * * @param mean the mean of the distribution * @return the random Exponential value */ public double nextExponential(double mean) { if (mean <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "mean must be positive ({0})", mean); } final RandomGenerator generator = getRan(); double unif = generator.nextDouble(); while (unif == 0.0d) { unif = generator.nextDouble(); } return -mean * Math.log(unif); }
/** * {@inheritDoc} * <p> * <strong>Algorithm Description</strong>: scales the output of * Random.nextDouble(), but rejects 0 values (i.e., will generate another * random double if Random.nextDouble() returns 0). This is necessary to * provide a symmetric output interval (both endpoints excluded). * </p> * * @param lower * the lower bound. * @param upper * the upper bound. * @return a uniformly distributed random value from the interval (lower, * upper) * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public double nextUniform(double lower, double upper) { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } final RandomGenerator generator = getRan(); // ensure nextDouble() isn't 0.0 double u = generator.nextDouble(); while (u <= 0.0) { u = generator.nextDouble(); } return lower + u * (upper - lower); }
/** * {@inheritDoc} * <p> * <strong>Algorithm Description</strong>: scales the output of * Random.nextDouble(), but rejects 0 values (i.e., will generate another * random double if Random.nextDouble() returns 0). This is necessary to * provide a symmetric output interval (both endpoints excluded). * </p> * * @param lower * the lower bound. * @param upper * the upper bound. * @return a uniformly distributed random value from the interval (lower, * upper) */ public double nextUniform(double lower, double upper) { if (lower >= upper) { throw MathRuntimeException.createIllegalArgumentException( "upper bound ({0}) must be greater than lower bound ({1})", upper, lower); } final RandomGenerator generator = getRan(); // ensure nextDouble() isn't 0.0 double u = generator.nextDouble(); while (u <= 0.0) { u = generator.nextDouble(); } return lower + u * (upper - lower); }
/** * Returns a random value from an Exponential distribution with the given * mean. * <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> * * @param mean the mean of the distribution * @return the random Exponential value * @throws NotStrictlyPositiveException if {@code mean <= 0}. */ public double nextExponential(double mean) { if (mean <= 0.0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean); } final RandomGenerator generator = getRan(); double unif = generator.nextDouble(); while (unif == 0.0d) { unif = generator.nextDouble(); } return -mean * FastMath.log(unif); }
final RandomGenerator generator = getRan();