java.util.SplittableRandom Java Examples

The following examples show how to use java.util.SplittableRandom. 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: FastAggregationRLEStressTest.java    From RoaringBitmap with Apache License 2.0 7 votes vote down vote up
@Setup(Level.Trial)
public void createBitmaps() {
  random = new SplittableRandom(seed);
  RoaringBitmapWriter<RoaringBitmap> bitmapWriter = constructionStrategy.create();
  bitmaps = new RoaringBitmap[count];
  bufferBitmaps = new ImmutableRoaringBitmap[count];
  double p = Math.pow(probability, 1D/count);
  for (int i = 0; i < count; ++i) {
    for (int j = (int)(Math.log(random.nextDouble())/Math.log(1-p));
         j < size;
         j += (int)(Math.log(random.nextDouble())/Math.log(1-p)) + 1) {
        bitmapWriter.add(j);
    }
    bitmaps[i] = bitmapWriter.get();
    bufferBitmaps[i] = bitmaps[i].toMutableRoaringBitmap();
    bitmapWriter.reset();
  }
}
 
Example #2
Source File: S3RecoverableFsDataOutputStreamTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static List<byte[]> createRandomLargeTestDataBuffers() {
	final List<byte[]> testData = new ArrayList<>();
	final SplittableRandom random = new SplittableRandom();

	long totalSize = 0L;

	int expectedSize = (int) random.nextLong(USER_DEFINED_MIN_PART_SIZE * 5L, USER_DEFINED_MIN_PART_SIZE * 100L);
	while (totalSize < expectedSize) {

		int len = random.nextInt(0, (int) (2L * USER_DEFINED_MIN_PART_SIZE));
		byte[] buffer = randomBuffer(random, len);
		totalSize += buffer.length;
		testData.add(buffer);
	}
	return testData;
}
 
Example #3
Source File: SplittableRandomTest.java    From hottub 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 TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #5
Source File: SplittableRandomTest.java    From hottub 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 #6
Source File: SplittableRandomTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #7
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 #8
Source File: SinCosPerformance.java    From commons-numbers with Apache License 2.0 6 votes vote down vote up
@Override
protected double[] createNumbers(SplittableRandom rng) {
    DoubleSupplier generator;
    if ("pi".equals(type)) {
        generator = () -> rng.nextDouble() * 2 * Math.PI - Math.PI;
    } else if ("pi/2".equals(type)) {
        generator = () -> rng.nextDouble() * Math.PI - Math.PI / 2;
    } else if ("random".equals(type)) {
        generator = () -> createRandomNumber(rng);
    } else if ("edge".equals(type)) {
        generator = () -> createEdgeNumber(rng);
    } else {
        throw new IllegalStateException("Unknown number type: " + type);
    }
    return DoubleStream.generate(generator).limit(getSize()).toArray();
}
 
Example #9
Source File: SplittableRandomTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #10
Source File: SplittableRandomTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #11
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private static List<byte[]> createRandomLargeTestDataBuffers() {
	final List<byte[]> testData = new ArrayList<>();
	final SplittableRandom random = new SplittableRandom();

	long totalSize = 0L;

	int expectedSize = (int) random.nextLong(USER_DEFINED_MIN_PART_SIZE * 5L, USER_DEFINED_MIN_PART_SIZE * 100L);
	while (totalSize < expectedSize) {

		int len = random.nextInt(0, (int) (2L * USER_DEFINED_MIN_PART_SIZE));
		byte[] buffer = randomBuffer(random, len);
		totalSize += buffer.length;
		testData.add(buffer);
	}
	return testData;
}
 
Example #12
Source File: SplittableRandomTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #13
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 #14
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel sized stream of doubles generates the given number of values
 */
public void testDoublesCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.doubles(size).parallel().forEach(x -> {counter.increment();});
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
 
Example #15
Source File: SplittableRandomTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel sized stream of ints generates the given number of values
 */
public void testIntsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.ints(size).parallel().forEach(x -> {counter.increment();});
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
 
Example #16
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
    assertEquals(counter.sum(), size);
}
 
Example #17
Source File: SplittableRandomTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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(fails.get(), 0);
}
 
Example #18
Source File: SplittableRandomTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A SplittableRandom produced by split() of a seeded-constructed
 * SplittableRandom generates a different sequence
 */
public void testSplit2() {
    SplittableRandom sr = new SplittableRandom(12345);
    for (int reps = 0; reps < REPS; ++reps) {
        SplittableRandom sc = sr.split();
        int i = 0;
        while (i < NCALLS && sr.nextLong() == sc.nextLong())
            ++i;
        assertTrue(i < NCALLS);
    }
}
 
Example #19
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
     * nextDouble(bound) throws IllegalArgumentException
     */
    public void testNextDoubleBadBound() {
        SplittableRandom sr = new SplittableRandom();
        executeAndCatchIAE(() -> sr.nextDouble(0.0));
        executeAndCatchIAE(() -> sr.nextDouble(-0.0));
        executeAndCatchIAE(() -> sr.nextDouble(+0.0));
        executeAndCatchIAE(() -> sr.nextDouble(-1.0));
        executeAndCatchIAE(() -> sr.nextDouble(Double.NaN));
        executeAndCatchIAE(() -> sr.nextDouble(Double.NEGATIVE_INFINITY));

        // Returns Double.MAX_VALUE
//        executeAndCatchIAE(() -> r.nextDouble(Double.POSITIVE_INFINITY));
    }
 
Example #20
Source File: SplittableRandomTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A SplittableRandom produced by split() of a default-constructed
 * SplittableRandom generates a different sequence
 */
public void testSplit1() {
    SplittableRandom sr = new SplittableRandom();
    for (int reps = 0; reps < REPS; ++reps) {
        SplittableRandom sc = sr.split();
        int i = 0;
        while (i < NCALLS && sr.nextLong() == sc.nextLong())
            ++i;
        assertTrue(i < NCALLS);
    }
}
 
Example #21
Source File: SplittableRandomTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel sized stream of ints generates the given number of values
 */
public void testIntsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.ints(size).parallel().forEach(x -> {counter.increment();});
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
 
Example #22
Source File: SplittableRandomTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #23
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #24
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #25
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 #26
Source File: SplittableRandomTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
    }
}
 
Example #27
Source File: SplittableRandomTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.ints().limit(size).parallel().forEach(x -> {counter.increment();});
    assertEquals(counter.sum(), size);
}
 
Example #28
Source File: SplittableRandomTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A SplittableRandom produced by split() of a seeded-constructed
 * SplittableRandom generates a different sequence
 */
public void testSplit2() {
    SplittableRandom sr = new SplittableRandom(12345);
    for (int reps = 0; reps < REPS; ++reps) {
        SplittableRandom sc = sr.split();
        int i = 0;
        while (i < NCALLS && sr.nextLong() == sc.nextLong())
            ++i;
        assertTrue(i < NCALLS);
    }
}
 
Example #29
Source File: SplittableRandomTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel unsized stream of doubles generates at least 100 values
 */
public void testUnsizedDoublesCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.doubles().limit(size).parallel().forEach(x -> {counter.increment();});
    assertEquals(counter.sum(), size);
}
 
Example #30
Source File: SplittableRandomTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A parallel unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
    assertEquals(counter.sum(), size);
}