@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); } }
@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]); } } }
private void assertWellBalanced(int numNodes, Multiset<Integer> ids) { // compute the chi-sq statistic double expected = ids.size() / (double) numNodes; double chiSq = 0.0; int df = numNodes - 1; NumberFormat prct = NumberFormat.getPercentInstance(); prct.setMaximumFractionDigits(4); NumberFormat num = NumberFormat.getInstance(); num.setMaximumFractionDigits(4); num.setMinimumFractionDigits(4); System.out.println("node\treqs\tX^2\tskew"); for(Integer id: ids.elementSet()) { System.out.println(id + "\t" + ids.count(id) + "\t" + num.format(chiSq(ids.count(id), expected)) + "\t" + prct.format((ids.count(id) - expected) / expected)); chiSq += chiSq(ids.count(id), expected); } System.out.println("X^2 = " + chiSq); ChiSquare dist = new ChiSquare(df, new MersenneTwister()); // p-value is ~= prob of seeing this distribution from fair router double pValue = 1.0 - dist.cdf(chiSq); System.out.println("p-value = " + pValue); assertTrue("Non-uniform load distribution detected.", pValue >= 0.05); }
@Test public void testComprehensiveOnMixture() { RandomEngine r = new MersenneTwister64(0); Normal[] dists = new Normal[]{ new Normal(100, 50, r),
/** * Returns a uniformly distributed random <tt>boolean</tt>. */ public boolean nextBoolean() { return randomGenerator.raw() > 0.5; } /**
RandomEngine engine = new DRand(); Poisson poisson = new Poisson(lambda, engine); int poissonObs = poisson.nextInt(); Normal normal = new Normal(mean, variance, engine); double normalObs = normal.nextDouble();
/** * 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)); } /**
/** * Constructs and returns a random number generator with the given seed. */ public MersenneTwister(int seed) { setSeed(seed); } /**
/** * Constructs and returns a random number generator with the given seed. * @param seed should not be 0, in such a case <tt>DRand.DEFAULT_SEED</tt> is substituted. */ public DRand(int seed) { setSeed(seed); } /**
/** * Returns the next seed. */ public int nextSeed() { return RandomSeedTable.getSeedAtRowColumn(row++, column); } /**
/** * 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 raw() { return nextDouble(); } }
/** * Constructs a uniform distribution with the given minimum and maximum, using a {@link cern.jet.random.engine.MersenneTwister} seeded with the given seed. */ public Uniform(double min, double max, int seed) { this(min,max, new cern.jet.random.engine.MersenneTwister(seed)); } /**
@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 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())); } /**
/** * Constructs a uniform distribution with the given minimum and maximum, using a {@link cern.jet.random.engine.MersenneTwister} seeded with the given seed. */ public Uniform(double min, double max, int seed) { this(min,max, new cern.jet.random.engine.MersenneTwister(seed)); } /**
@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); }
/** 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(); } /**
@Test public void testGeometricDistribution() { StreamSummary<Integer> vs = new StreamSummary<Integer>(10); 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); assertEquals(0, tippyTop); System.out.println(vs); }
@Test public void testGeometricDistribution() { ConcurrentStreamSummary<Integer> vs = new ConcurrentStreamSummary<Integer>(10); 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); assertEquals(0, tippyTop); System.out.println(vs); }
/** * Constructs and returns a new uniform random number generation engine seeded with the current time. * Currently this is {@link cern.jet.random.engine.MersenneTwister}. */ public static RandomEngine makeDefaultGenerator() { return cern.jet.random.engine.RandomEngine.makeDefault(); } /**