/** * {@inheritDoc} * * <p><strong>Algorithm Description</strong>: if the lower bound is excluded, 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). * * @throws NumberIsTooLargeException if {@code lower >= upper} * @throws NotFiniteNumberException if one of the bounds is infinite * @throws NotANumberException if one of the bounds is NaN */ public double nextUniform(double lower, double upper, boolean lowerInclusive) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException { if (lower >= upper) { throw new NumberIsTooLargeException( LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } if (Double.isInfinite(lower)) { throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower); } if (Double.isInfinite(upper)) { throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper); } if (Double.isNaN(lower) || Double.isNaN(upper)) { throw new NotANumberException(); } final RandomGenerator generator = getRandomGenerator(); // ensure nextDouble() isn't 0.0 double u = generator.nextDouble(); while (!lowerInclusive && u <= 0.0) { u = generator.nextDouble(); } return u * upper + (1.0 - u) * 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. * * @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); }
/** * <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). * * @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 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(); return lower + (long) (rand.nextDouble() * (upper - lower + 1)); }
/** * 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(); return lower + (int) (rand.nextDouble() * (upper - lower + 1)); }
private void getVelocity() { vy = 3.0; vx = rgen.nextDouble(1.0, 3.0); if (rgen.nextBoolean(0.5)) { vx = -vx; } }
protected void setRandomValue(double a_value) { RandomGenerator randomGen = getGPConfiguration().getRandomGenerator(); m_value_double = randomGen.nextDouble() * (m_upperBounds - m_lowerBounds) + m_lowerBounds; if (m_wholeNumbers) { m_value_double = Math.round(m_value_double); } }
public Individual[] reproduce(Individual[] parents, GAParameterSet params) { if (parents.length != getRequiredNumberOfParents()) throw new IllegalArgumentException( "Need " + getRequiredNumberOfParents() + " parents for reproduction (not " + parents.length + ")"); // Check correct type for parents, get length: int bitLen = checkParentsTypeAndLength(parents); // Chance (1 - xover probability) that parents wont be changed: final RandomGenerator rnd = params.getRandomGenerator(); if (rnd.nextDouble() >= getXOverProbability()) return makeCopyOfParents(parents, params); // Get parents bitsrings: BitString p1 = ((BinaryEncodedIndividual) parents[0]).getBitStringRepresentation(); BitString p2 = ((BinaryEncodedIndividual) parents[1]).getBitStringRepresentation(); // x-over: final int maxAttempts = params.getMaxBadReproductionAttempts(); int attempts = 0; boolean kidsAreValid = false; do { kidsAreValid = false; int xPoint = rnd.nextInt(1, bitLen); // offspring bit strings: BitString c1 = new BitString(bitLen); BitString c2 = new BitString(bitLen); // copy before xover-point: for (int i = 0; i < xPoint; i++) { c1.set(i, p1.get(i)); c2.set(i, p2.get(i)); } // copy after xover-point: for (int i = xPoint; i < bitLen; i++) { c1.set(i, p2.get(i)); c2.set(i, p1.get(i)); } // create children and check if children are valid: ClassifierIndividual[] kids = createKidsFromEncoding(params, c1, c2); kidsAreValid = kidsSatisfyConstraints(kids, params); // return valid kids or have another attempts: if (kidsAreValid) return kids; else attempts++; } while (!kidsAreValid && attempts < maxAttempts); // all attempts failed: return makeCopyOfParents(parents, params); }
/** When mouse pressed: begin our fantastic game */ public void mousePressed(MouseEvent e) { if (vx == 0) { vx = rgen.nextDouble(0.5, 1); vy = -1; if (rgen.nextBoolean() == true) { vx *= -1; } } }
/** * Generates a random long value from the Poisson distribution with the given mean. * * <p><strong>Algorithm Description</strong>: Uses simulation of a Poisson process using Uniform * deviates, as described <a href="http://irmi.epfl.ch/cmos/Pmmi/interactive/rng7.htm">here.</a> * * <p>The Poisson process (and hence value returned) is bounded by 1000 * mean. * * @param mean mean of the Poisson distribution. * @return the random Poisson value. */ public long nextPoisson(double mean) { if (mean <= 0) { throw new IllegalArgumentException("Poisson mean must be > 0"); } double p = Math.exp(-mean); long n = 0; double r = 1.0d; double rnd = 1.0d; RandomGenerator rand = getRan(); while (n < 1000 * mean) { rnd = rand.nextDouble(); r = r * rnd; if (r >= p) { n++; } else { return n; } } return n; }
protected void setRandomValue(long a_value) { RandomGenerator randomGen = getGPConfiguration().getRandomGenerator(); m_value_long = Math.round(randomGen.nextDouble() * (m_upperBounds - m_lowerBounds) + m_lowerBounds); }