Search in sources :

Example 36 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testNextIntBounded2.

/**
     * 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);
        }
    }
}
Also used : SplittableRandom(java.util.SplittableRandom)

Example 37 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method longsDataProvider.

@DataProvider(name = "longs")
public static Object[][] longsDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Long>, LongStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToLong(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs().limit(%d)", SIZE), () -> new SplittableRandom().longs().limit(SIZE)), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
    data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d)", SIZE), () -> new SplittableRandom().longs(SIZE)), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
    data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong())", SIZE), () -> rbsf.apply(sr -> sr.nextLong())), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final long origin = o;
            final long bound = b;
            data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().longs(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().longs(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextLong(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextLong(origin, bound))), randomAsserter(SIZE, origin, bound) });
        }
    }
    return data.toArray(new Object[0][]);
}
Also used : IntStream(java.util.stream.IntStream) LongStream(java.util.stream.LongStream) DataProvider(org.testng.annotations.DataProvider) DoubleStreamTestScenario(java.util.stream.DoubleStreamTestScenario) TestData(java.util.stream.TestData) Set(java.util.Set) Test(org.testng.annotations.Test) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) OpTestCase(java.util.stream.OpTestCase) SplittableRandom(java.util.SplittableRandom) StreamSupport(java.util.stream.StreamSupport) Spliterator(java.util.Spliterator) IntStreamTestScenario(java.util.stream.IntStreamTestScenario) LongStreamTestScenario(java.util.stream.LongStreamTestScenario) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LongStream(java.util.stream.LongStream) SplittableRandom(java.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Example 38 with SplittableRandom

use of java.util.SplittableRandom in project graal by oracle.

the class IdentityHashCodeSupport method generateHashCode.

protected static int generateHashCode() {
    SplittableRandom hashCodeGenerator = hashCodeGeneratorTL.get();
    if (hashCodeGenerator == null) {
        /*
             * Create a new thread-local random number generator. SplittableRandom ensures that
             * values created by different random number generator instances are random as a whole.
             */
        hashCodeGenerator = new SplittableRandom();
        hashCodeGeneratorTL.set(hashCodeGenerator);
    }
    int hashCode;
    do {
        hashCode = hashCodeGenerator.nextInt();
    } while (hashCode == 0);
    return hashCode;
}
Also used : SplittableRandom(java.util.SplittableRandom)

Example 39 with SplittableRandom

use of java.util.SplittableRandom in project flink by apache.

the class S3RecoverableFsDataOutputStreamTest method createRandomLargeTestDataBuffers.

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;
}
Also used : ArrayList(java.util.ArrayList) SplittableRandom(java.util.SplittableRandom)

Aggregations

SplittableRandom (java.util.SplittableRandom)39 LongAdder (java.util.concurrent.atomic.LongAdder)9 Test (org.testng.annotations.Test)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Spliterator (java.util.Spliterator)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 Function (java.util.function.Function)3 DoubleStream (java.util.stream.DoubleStream)3 DoubleStreamTestScenario (java.util.stream.DoubleStreamTestScenario)3 IntStream (java.util.stream.IntStream)3 IntStreamTestScenario (java.util.stream.IntStreamTestScenario)3 LongStream (java.util.stream.LongStream)3 LongStreamTestScenario (java.util.stream.LongStreamTestScenario)3 OpTestCase (java.util.stream.OpTestCase)3 StreamSupport (java.util.stream.StreamSupport)3 TestData (java.util.stream.TestData)3