@Test public void testRandomEngine() { int[] maxcounts = new int[10]; int[] counts = new int[20]; RandomEngine re = RandomEngine.makeDefault(); for (int i = 0; i < NUM_ITERATIONS; i++) { // int z = Distributions.nextZipfInt(1.2D, re); int z = Distributions.nextGeometric(0.25, re); if (z > Integer.MAX_VALUE - 9) { maxcounts[Integer.MAX_VALUE - z]++; } if (z < 20) { counts[z]++; } } for (int i = 0; i < 20; i++) { System.out.println(i + ": " + counts[i]); } for (int i = 9; i >= 0; i--) { System.out.println((Integer.MAX_VALUE - i) + ": " + maxcounts[i]); } } }
@Override public void doOperation(int index) throws Exception { int req; if(random.nextFloat() < cachePercent && index > 1) { int maxValid = Math.min(Math.min(Math.max(cache.length, index - 1), uncached.get() - 1), cache.length); int cacheIndex = Math.abs(random.nextInt()) % maxValid; req = cache[cacheIndex]; } else { req = Math.abs(random.nextInt()) % size; cache[uncached.getAndIncrement() % cache.length] = req; } test.doOperation(req); } }
/** Equivalent to <tt>raw()</tt>. This has the effect that random engines can now be used as function objects, returning a random number upon function evaluation. */ public double apply(double dummy) { return raw(); } /**
/** * Returns a 64 bit uniformly distributed random number in the closed * interval <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt> (including * <tt>Long.MIN_VALUE</tt> and <tt>Long.MAX_VALUE</tt>). */ public long nextLong() { // concatenate two 32-bit strings into one 64-bit string return ((nextInt() & 0xFFFFFFFFL) << 32) | ((nextInt() & 0xFFFFFFFFL)); }
/** * Returns a copy of the receiver; the copy will produce identical sequences. * After this call has returned, the copy and the receiver have equal but separate state. * * @return a copy of the receiver. */ public Object clone() { MersenneTwister clone = (MersenneTwister) super.clone(); clone.mt = (int[]) this.mt.clone(); return clone; } /**
/** * Returns a 64 bit uniformly distributed random number in the open unit * interval <code>(0.0,1.0)</code> (excluding 0.0 and 1.0). */ public double nextDouble() { double nextDouble; do { // -9.223372036854776E18 == (double) Long.MIN_VALUE // 5.421010862427522E-20 == 1 / Math.pow(2,64) == 1 / ((double) // Long.MAX_VALUE - (double) Long.MIN_VALUE); nextDouble = ((double) nextLong() - -9.223372036854776E18) * 5.421010862427522E-20; } // catch loss of precision of long --> double conversion while (!(nextDouble > 0.0 && nextDouble < 1.0)); // --> in (0.0,1.0) return nextDouble; /* * nextLong == Long.MAX_VALUE --> 1.0 nextLong == Long.MIN_VALUE --> 0.0 * nextLong == Long.MAX_VALUE-1 --> 1.0 nextLong == * Long.MAX_VALUE-100000L --> 0.9999999999999946 nextLong == * Long.MIN_VALUE+1 --> 0.0 nextLong == Long.MIN_VALUE-100000L --> * 0.9999999999999946 nextLong == 1L --> 0.5 nextLong == -1L --> 0.5 * nextLong == 2L --> 0.5 nextLong == -2L --> 0.5 nextLong == 2L+100000L * --> 0.5000000000000054 nextLong == -2L-100000L --> * 0.49999999999999456 */ }
/** * Returns a uniformly distributed random <tt>boolean</tt>. */ public boolean nextBoolean() { return randomGenerator.raw() > 0.5; } /**
/** * Returns a 64 bit uniformly distributed random number in the closed interval <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt> (including <tt>Long.MIN_VALUE</tt> and <tt>Long.MAX_VALUE</tt>). */ public long nextLong() { // concatenate two 32-bit strings into one 64-bit string return ((nextInt() & 0xFFFFFFFFL) << 32) | ((nextInt() & 0xFFFFFFFFL)); } /**
/** * Returns a deep copy of the receiver; the copy will produce identical sequences. * After this call has returned, the copy and the receiver have equal but separate state. * * @return a copy of the receiver. */ public Object clone() { AbstractDistribution copy = (AbstractDistribution) super.clone(); if (this.randomGenerator != null) copy.randomGenerator = (RandomEngine) this.randomGenerator.clone(); return copy; } /**
nextDouble = ((double) nextLong() - -9.223372036854776E18) * 5.421010862427522E-20;
/** * Returns a uniformly distributed random number in the closed interval <tt>[from,to]</tt> (including <tt>from</tt> and <tt>to</tt>). * Pre conditions: <tt>from <= to</tt>. */ public int nextIntFromTo(int from, int to) { return (int) ((long)from + (long)((1L + (long)to - (long)from)*randomGenerator.raw())); } /**
/** * Equivalent to <tt>nextInt()</tt>. This has the effect that random engines * can now be used as function objects, returning a random number upon * function evaluation. */ public int apply(int dummy) { return nextInt(); }
@Test public void testZipfianDistribution() { RandomEngine re = RandomEngine.makeDefault(); for (int i = 0; i < NUM_ITERATIONS; i++) { int z = Distributions.nextZipfInt(1.2D, re); vs.offer(z); } List<Integer> top = vs.peek(5); System.out.println("Zipfian:"); for (Integer e : top) { System.out.println(e); } int tippyTop = top.get(0); assertTrue(tippyTop < 3); }
/** * Returns a deep copy of the receiver. */ public Object clone() { RandomSampler copy = (RandomSampler) super.clone(); copy.my_RandomGenerator = (RandomEngine) this.my_RandomGenerator.clone(); return copy; } /**
nextDouble = ((double) nextLong() - -9.223372036854776E18) * 5.421010862427522E-20;
/** * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt> (excluding <tt>from</tt> and <tt>to</tt>). * Pre conditions: <tt>from <= to</tt>. */ public double nextDoubleFromTo(double from, double to) { return from+(to-from)*randomGenerator.raw(); } /**
/** Equivalent to <tt>nextInt()</tt>. This has the effect that random engines can now be used as function objects, returning a random number upon function evaluation. */ public int apply(int dummy) { return nextInt(); } /**
@Test public void testGeometricDistribution() { RandomEngine re = RandomEngine.makeDefault(); for (int i = 0; i < NUM_ITERATIONS; i++) { int z = Distributions.nextGeometric(0.25, re); vs.offer(z); } List<Integer> top = vs.peek(5); System.out.println("Geometric:"); for (Integer e : top) { System.out.println(e); } int tippyTop = top.get(0); assertTrue(tippyTop < 3); }