/** * Generates randomStr random string (for Multipart requests) * @param lenght the char number of the random string * @return the random string */ public static String generateRandomString(int lenght) { SplittableRandom splittableRandom = new SplittableRandom(); StringBuffer randomStr = new StringBuffer(); int randInt, temp; for (int i = 0; i < lenght; i++) { randInt = splittableRandom.nextInt(0, 2); if (randInt == 1) { temp = splittableRandom.nextInt('A', 'Z'); } else { temp = splittableRandom.nextInt('a', 'z'); } randomStr.append((char) temp); } return randomStr.toString(); }
/** * {@inheritDoc} Applies only to the calling thread. */ @SuppressWarnings("contracts.postcondition.not.satisfied") @Override public void setSeed( final long seed) { if (this.seed == null) { super.setSeed(seed); } if (splittableRandoms != null) { splittableRandoms.set(new SplittableRandom(seed)); if (entropyBits != null) { entropyBits.get().updateAndGet(oldValue -> Math.max(oldValue, SEED_LENGTH_BITS)); } if (seeds != null) { seeds.set(BinaryUtils.convertLongToBytes(seed)); } } }
/** * Generates a random string (for Multipart requests) * @param lenght the char number of the random string * @return the random string */ public static String generateRandomString(int lenght) { SplittableRandom splittableRandom = new SplittableRandom(); StringBuffer a = new StringBuffer(); int nextInt, temp; for (int i = 0; i < lenght; i++) { nextInt = splittableRandom.nextInt(0, 2); temp = 'a'; if (nextInt == 1) { temp = splittableRandom.nextInt('A', 'Z'); } else { temp = splittableRandom.nextInt('a', 'z'); } a.append((char) temp); } return a.toString(); }
/** * nextInt(bound) returns 0 <= value < bound; * repeated calls produce at least two distinct results */ public void testNextIntBounded() { SplittableRandom sr = new SplittableRandom(); // sample bound space across prime number increments for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { int f = sr.nextInt(bound); assertTrue(0 <= f && f < bound); int i = 0; int j; while (i < NCALLS && (j = sr.nextInt(bound)) == f) { assertTrue(0 <= j && j < bound); ++i; } assertTrue(i < NCALLS); } }
/** * nextInt(least, bound) returns least <= value < bound; * repeated calls produce at least two distinct results */ public void testNextIntBounded2() { SplittableRandom sr = new SplittableRandom(); for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) { int f = sr.nextInt(least, bound); assertTrue(least <= f && f < bound); int i = 0; int j; while (i < NCALLS && (j = sr.nextInt(least, bound)) == f) { assertTrue(least <= j && j < bound); ++i; } assertTrue(i < NCALLS); } } }
/** * nextLong(bound) returns 0 <= value < bound; * repeated calls produce at least two distinct results */ public void testNextLongBounded() { SplittableRandom sr = new SplittableRandom(); for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { long f = sr.nextLong(bound); assertTrue(0 <= f && f < bound); int i = 0; long j; while (i < NCALLS && (j = sr.nextLong(bound)) == f) { assertTrue(0 <= j && j < bound); ++i; } assertTrue(i < NCALLS); } }
/** * nextLong(least, bound) returns least <= value < bound; * repeated calls produce at least two distinct results */ public void testNextLongBounded2() { SplittableRandom sr = new SplittableRandom(); for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { long f = sr.nextLong(least, bound); assertTrue(least <= f && f < bound); int i = 0; long j; while (i < NCALLS && (j = sr.nextLong(least, bound)) == f) { assertTrue(least <= j && j < bound); ++i; } assertTrue(i < NCALLS); } } }
/** * nextDouble(least, bound) returns least <= value < bound; * repeated calls produce at least two distinct results */ public void testNextDoubleBounded2() { SplittableRandom sr = new SplittableRandom(); for (double least = 0.0001; least < 1.0e20; least *= 8) { for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) { double f = sr.nextDouble(least, bound); assertTrue(least <= f && f < bound); int i = 0; double j; while (i < NCALLS && (j = sr.nextDouble(least, bound)) == f) { assertTrue(least <= j && j < bound); ++i; } assertTrue(i < NCALLS); } } }
static void test(int i, int nkeys, Class mapClass) throws Exception { System.out.print("Threads: " + i + "\t:"); Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance(); Integer[] key = makeKeys(nkeys); // Uncomment to start with a non-empty table // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied // map.put(key[j], key[j]); LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier barrier = new CyclicBarrier(i+1, timer); SplittableRandom rnd = new SplittableRandom(); for (int t = 0; t < i; ++t) pool.execute(new Runner(map, key, barrier, rnd.split())); barrier.await(); barrier.await(); long time = timer.getTime(); long tpo = time / (i * (long)nops); System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); double secs = (double)(time) / 1000000000.0; System.out.println("\t " + secs + "s run time"); map.clear(); }
/** * Each of a parallel sized stream of bounded ints is within bounds */ public void testBoundedInts() { AtomicInteger fails = new AtomicInteger(0); SplittableRandom r = new SplittableRandom(); long size = 12345L; for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { final int lo = least, hi = bound; r.ints(size, lo, hi).parallel().forEach( x -> { if (x < lo || x >= hi) fails.getAndIncrement(); }); } } assertEquals(0, fails.get()); }
/** * Each of a parallel sized stream of bounded longs is within bounds */ public void testBoundedLongs() { AtomicInteger fails = new AtomicInteger(0); SplittableRandom r = new SplittableRandom(); long size = 123L; for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { final long lo = least, hi = bound; r.longs(size, lo, hi).parallel().forEach( x -> { if (x < lo || x >= hi) fails.getAndIncrement(); }); } } assertEquals(0, fails.get()); }
/** * Each of a parallel sized stream of bounded doubles is within bounds */ public void testBoundedDoubles() { AtomicInteger fails = new AtomicInteger(0); SplittableRandom r = new SplittableRandom(); long size = 456; for (double least = 0.00011; least < 1.0e20; least *= 9) { for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { final double lo = least, hi = bound; r.doubles(size, lo, hi).parallel().forEach( x -> { if (x < lo || x >= hi) fails.getAndIncrement(); }); } } assertEquals(0, fails.get()); }
/** * Generates a random string (for Multipart requests) * @param lenght the char number of the random string * @return the random string */ public static String generateRandomString(int lenght) { SplittableRandom splittableRandom = new SplittableRandom(); StringBuffer a = new StringBuffer(); int nextInt, ext; for (int i = 0; i < lenght; i++) { nextInt = splittableRandom.nextInt(0, 2); ext = 'a'; if (nextInt == 1) { ext = splittableRandom.nextInt('A', 'Z'); } else { ext = splittableRandom.nextInt('a', 'z'); } a.append((char) ext); } return a.toString(); }
/** * Randomizes the alive state of the cells. */ public void randomize() { SplittableRandom random = new SplittableRandom(); int rows = getRows(); int columns = getColumns(); boolean[][] aliveMatrix = new boolean[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { aliveMatrix[i][j] = random.nextInt() >= 0; } } board.setAliveMatrix(aliveMatrix); draw(); }
/** Delegates to {@link SplittableRandom#nextInt(int) SplittableRandom.nextInt(256)}. */ @SuppressWarnings("NumericCastThatLosesPrecision") @Override public void nextBytes( final byte[] bytes) { final SplittableRandom local = getSplittableRandom(); for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) (local.nextInt(256)); } debitEntropy(bytes.length * (long) (Byte.SIZE)); }
/** * Replaces {@link #underlying} with a new {@link SplittableRandom} that uses the given seed. */ @Override protected void setSeedInternal(final byte[] seed) { if (seed.length != Long.BYTES) { throw new IllegalArgumentException("DirectSplittableRandomAdapter requires an 8-byte seed"); } super.setSeedInternal(seed); underlying = new SplittableRandom(BinaryUtils.convertBytesToLong(seed)); }
/** * Replaces {@link #underlying} with a new {@link SplittableRandom} that uses the given seed. */ @Override public void setSeed(final long seed) { if (superConstructorFinished) { super.setSeedInternal(BinaryUtils.convertLongToBytes(seed)); underlying = new SplittableRandom(seed); } }
default public List<T> sample(int maxSize) { SplittableRandom rnd = new SplittableRandom(); return IntStream .rangeClosed(1, maxSize) .boxed() .map(size -> generate(size, rnd)) .collect(Collectors.toList()); }
public default Result check() { SplittableRandom rnd = new SplittableRandom(); return IntStream .rangeClosed(1, 100) .boxed() .map(size -> run(size, rnd)) .collect(Collectors.reducing(Result.successful(), Result::merge)); }
/** * Repeated calls to nextInt produce at least two distinct results */ public void testNextInt() { SplittableRandom sr = new SplittableRandom(); int f = sr.nextInt(); int i = 0; while (i < NCALLS && sr.nextInt() == f) ++i; assertTrue(i < NCALLS); }
/** * Repeated calls to nextLong produce at least two distinct results */ public void testNextLong() { SplittableRandom sr = new SplittableRandom(); long f = sr.nextLong(); int i = 0; while (i < NCALLS && sr.nextLong() == f) ++i; assertTrue(i < NCALLS); }
/** * Repeated calls to nextDouble produce at least two distinct results */ public void testNextDouble() { SplittableRandom sr = new SplittableRandom(); double f = sr.nextDouble(); int i = 0; while (i < NCALLS && sr.nextDouble() == f) ++i; assertTrue(i < NCALLS); }
/** * Two SplittableRandoms created with the same seed produce the * same values for nextLong. */ public void testSeedConstructor() { for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { SplittableRandom sr1 = new SplittableRandom(seed); SplittableRandom sr2 = new SplittableRandom(seed); for (int i = 0; i < REPS; ++i) assertEquals(sr1.nextLong(), sr2.nextLong()); } }