Java Code Examples for java.util.SplittableRandom#nextInt()

The following examples show how to use java.util.SplittableRandom#nextInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SplittableRandomTest.java    From jdk8u-jdk with GNU General Public License v2.0 7 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 2
Source File: SplittableRandomTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: SplittableRandomTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 4
Source File: SplittableRandomTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: SplittableRandomTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 6
Source File: SplittableRandomTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 7
Source File: SplittableRandomTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 8
Source File: SeededTestData.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
private static int[] createSorted16BitInts(int howMany, SplittableRandom random) {
  // we can have at most 65536 keys in a RoaringBitmap
  long[] bitset = bits.get();
  Arrays.fill(bitset, 0L);
  int consumed = 0;
  while (consumed < howMany) {
    int value = random.nextInt(1 << 16);
    long bit = (1L << value);
    consumed += 1 - Long.bitCount(bitset[value >>> 6] & bit);
    bitset[value >>> 6] |= bit;
  }
  int[] keys = new int[howMany];
  int prefix = 0;
  int k = 0;
  for (int i = bitset.length - 1; i >= 0; --i) {
    long word = bitset[i];
    while (word != 0) {
      keys[k++] = prefix + Long.numberOfTrailingZeros(word);
      word &= (word - 1);
    }
    prefix += 64;
  }
  return keys;
}
 
Example 9
Source File: MapLoops.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void shuffleKeys(Integer[] key) {
    SplittableRandom rnd = new SplittableRandom();
    for (int i = key.length; i > 1; --i) {
        int j = rnd.nextInt(i);
        Integer tmp = key[j];
        key[j] = key[i-1];
        key[i-1] = tmp;
    }
}
 
Example 10
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: BSBFSDDeDuplicator.java    From PDD with Apache License 2.0 5 votes vote down vote up
private void setHashBuffer(BitArray[] bloomFilters, int[] hashBuffer, SplittableRandom random) {
    final int hashBufferLength = hashBuffer.length;
    final BitArray randomBloomFilter = bloomFilters[random.nextInt(hashBufferLength)];
    randomBloomFilter.clear(random.nextLong(randomBloomFilter.bitSize()));
    for (int index = 0; index < hashBufferLength; index++) {
        final int combinedHash = hashBuffer[index];
        final BitArray bloomFilter = bloomFilters[index];
        bloomFilter.set(combinedHash % bloomFilter.bitSize());
    }
}
 
Example 12
Source File: SplittableRandomTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static byte[] randomBuffer(SplittableRandom random, int len) {
	byte[] buffer = new byte[len];
	for (int i = 0; i < buffer.length; i++) {
		buffer[i] = (byte) (random.nextInt() & 0xFF);
	}
	return buffer;
}
 
Example 14
Source File: SeededTestData.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
private static IntStream rleRegion(SplittableRandom random) {
  int numRuns = random.nextInt(1, 2048);
  int[] runs = createSorted16BitInts(numRuns * 2, random);
  return IntStream.range(0, numRuns)
          .map(i -> i * 2)
          .mapToObj(i -> IntStream.range(runs[i], runs[i + 1]))
          .flatMapToInt(i -> i);
}
 
Example 15
Source File: SingleWriterService.java    From hivemq-community-edition with Apache License 2.0 4 votes vote down vote up
public void run() {
    try {

        final SplittableRandom random = RANDOM.split();

        // It is possible that all tasks stop running while there are still non-empty queues.
        // We have yet to determine if there is a lock free way to avoid this.
        outerLoop:
        while (nonemptyQueueCounter.get() >= runningThreadsCount.getAndDecrement()) {
            runningThreadsCount.incrementAndGet();
            final long countSnapShot = globalTaskCount.get();
            if (countSnapShot == 0) {
                continue;
            }

            // Calculate the percentage portion of total tasks per persistence.
            for (int i = 0; i < producers.length; i++) {
                probabilities[i] = (int) ((producers[i].getTaskCount().get() * 100) / countSnapShot);
            }

            int sumWithoutMins = 0;
            // Set to min probability if necessary
            for (int i = 0; i < probabilities.length; i++) {
                if (probabilities[i] < MIN_PROBABILITY_IN_PERCENT) {
                    probabilities[i] = MIN_PROBABILITY_IN_PERCENT;
                } else {
                    sumWithoutMins += probabilities[i];
                }
            }

            int surplus = 0;
            for (int i = 0; i < probabilities.length; i++) {
                surplus += probabilities[i];
            }
            surplus -= 100;

            if (surplus > 0) { // Normalize to a 100% sum
                // We reduce the probability of all persistences that are not at the minimum, be a portion of the overhead.
                // The portion is based on there portion of the sum of all probabilities, ignoring those with minimum probability.
                for (int i = 0; i < probabilities.length; i++) {
                    if (probabilities[i] > MIN_PROBABILITY_IN_PERCENT) {
                        probabilities[i] -= surplus / (sumWithoutMins / probabilities[i]);
                    }
                }
            }

            final int randomInt = random.nextInt(100);
            int offset = 0;

            for (int i = 0; i < probabilities.length; i++) {
                if (randomInt <= probabilities[i] + offset) {
                    producers[i].execute(random);
                    continue outerLoop;
                }
                offset += probabilities[i];
            }
        }

    } catch (final Throwable t) {
        // Exceptions in the executed tasks, are passed to there result future.
        // So we only end up here if there is an exception in the probability calculation.
        // We decrement the running thread count so that a new thread will start running, as soon as a new task is added to any queue.
        runningThreadsCount.decrementAndGet();
        Exceptions.rethrowError("Exception in single writer executor. ", t);
    }
}
 
Example 16
Source File: SplittableRandomTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * nextInt(least >= bound) throws IllegalArgumentException
 */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBadBounds() {
    SplittableRandom sr = new SplittableRandom();
    int f = sr.nextInt(17, 2);
}
 
Example 17
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * nextInt(negative) throws IllegalArgumentException
 */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBoundedNeg() {
    SplittableRandom sr = new SplittableRandom();
    int f = sr.nextInt(-17);
}
 
Example 18
Source File: SplittableRandomTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * nextInt(least >= bound) throws IllegalArgumentException
 */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBadBounds() {
    SplittableRandom sr = new SplittableRandom();
    int f = sr.nextInt(17, 2);
}
 
Example 19
Source File: SplittableRandomTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * nextInt(negative) throws IllegalArgumentException
 */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBoundedNeg() {
    SplittableRandom sr = new SplittableRandom();
    int f = sr.nextInt(-17);
}
 
Example 20
Source File: SplittableRandomTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * nextInt(least >= bound) throws IllegalArgumentException
 */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBadBounds() {
    SplittableRandom sr = new SplittableRandom();
    int f = sr.nextInt(17, 2);
}