// max numbers of calls to detect getting stuck on one value staticfinalint NCALLS = 10000;
// max sampled int bound staticfinalint MAX_INT_BOUND = (1 << 28);
// max sampled long bound staticfinallong MAX_LONG_BOUND = (1L << 42);
// Number of replications for other checks staticfinalint REPS = 20;
/** *RepeatedcallstonextIntproduceatleasttwodistinctresults
*/ publicvoid testNextInt() {
Random r = new Random(); int f = r.nextInt(); int i = 0; while (i < NCALLS && r.nextInt() == f)
++i;
assertTrue(i < NCALLS);
}
/** *RepeatedcallstonextLongproduceatleasttwodistinctresults
*/ publicvoid testNextLong() {
Random r = new Random(); long f = r.nextLong(); int i = 0; while (i < NCALLS && r.nextLong() == f)
++i;
assertTrue(i < NCALLS);
}
/** *RepeatedcallstonextBooleanproduceatleasttwodistinctresults
*/ publicvoid testNextBoolean() {
Random r = new Random(); boolean f = r.nextBoolean(); int i = 0; while (i < NCALLS && r.nextBoolean() == f)
++i;
assertTrue(i < NCALLS);
}
/** *RepeatedcallstonextFloatproduceatleasttwodistinctresults
*/ publicvoid testNextFloat() {
Random r = new Random(); float f = r.nextFloat(); int i = 0; while (i < NCALLS && r.nextFloat() == f)
++i;
assertTrue(i < NCALLS);
}
/** *RepeatedcallstonextDoubleproduceatleasttwodistinctresults
*/ publicvoid testNextDouble() {
Random r = new Random(); double f = r.nextDouble(); int i = 0; while (i < NCALLS && r.nextDouble() == f)
++i;
assertTrue(i < NCALLS);
}
/** *RepeatedcallstonextGaussianproduceatleasttwodistinctresults
*/ publicvoid testNextGaussian() {
Random r = new Random(); double f = r.nextGaussian(); int i = 0; while (i < NCALLS && r.nextGaussian() == f)
++i;
assertTrue(i < NCALLS);
}
/** *nextInt(negative)throwsIllegalArgumentException
*/
@Test(expectedExceptions = IllegalArgumentException.class) publicvoid testNextIntBoundedNeg() {
Random r = new Random(); int f = r.nextInt(-17);
}
/** *nextInt(bound)returns0<=value<bound;repeatedcallsproduceat *leasttwodistinctresults
*/ publicvoid testNextIntBounded() {
Random r = new Random(); // sample bound space across prime number increments for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { int f = r.nextInt(bound);
assertTrue(0 <= f && f < bound); int i = 0; int j; while (i < NCALLS &&
(j = r.nextInt(bound)) == f) {
assertTrue(0 <= j && j < bound);
++i;
}
assertTrue(i < NCALLS);
}
}
/** *Asequentialsizedstreamofintsgeneratesthegivennumberofvalues
*/ publicvoid testIntsCount() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 0; for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.ints(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
size += 524959;
}
}
/** *Asequentialsizedstreamoflongsgeneratesthegivennumberofvalues
*/ publicvoid testLongsCount() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 0; for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.longs(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
size += 524959;
}
}
/** *Asequentialsizedstreamofdoublesgeneratesthegivennumberofvalues
*/ publicvoid testDoublesCount() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 0; for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.doubles(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
size += 524959;
}
}
/** *Eachofasequentialsizedstreamofboundedintsiswithinbounds
*/ publicvoid testBoundedInts() {
AtomicInteger fails = new AtomicInteger(0);
Random r = new Random(); 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) { finalint lo = least, hi = bound;
r.ints(size, lo, hi).
forEach(x -> { if (x < lo || x >= hi)
fails.getAndIncrement();
});
}
}
assertEquals(fails.get(), 0);
}
/** *Eachofasequentialsizedstreamofboundedlongsiswithinbounds
*/ publicvoid testBoundedLongs() {
AtomicInteger fails = new AtomicInteger(0);
Random r = new Random(); 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)) { finallong lo = least, hi = bound;
r.longs(size, lo, hi).
forEach(x -> { if (x < lo || x >= hi)
fails.getAndIncrement();
});
}
}
assertEquals(fails.get(), 0);
}
/** *Eachofasequentialsizedstreamofboundeddoublesiswithinbounds
*/ publicvoid testBoundedDoubles() {
AtomicInteger fails = new AtomicInteger(0);
Random r = new Random(); long size = 456; for (double least = 0.00011; least < 1.0e20; least *= 9) { for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { finaldouble lo = least, hi = bound;
r.doubles(size, lo, hi).
forEach(x -> { if (x < lo || x >= hi)
fails.getAndIncrement();
});
}
}
assertEquals(fails.get(), 0);
}
/** *Aparallelunsizedstreamofintsgeneratesatleast100values
*/ publicvoid testUnsizedIntsCount() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 100;
r.ints().limit(size).parallel().forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
/** *Aparallelunsizedstreamoflongsgeneratesatleast100values
*/ publicvoid testUnsizedLongsCount() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 100;
r.longs().limit(size).parallel().forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
/** *Aparallelunsizedstreamofdoublesgeneratesatleast100values
*/ publicvoid testUnsizedDoublesCount() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 100;
r.doubles().limit(size).parallel().forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
/** *Asequentialunsizedstreamofintsgeneratesatleast100values
*/ publicvoid testUnsizedIntsCountSeq() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 100;
r.ints().limit(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
/** *Asequentialunsizedstreamoflongsgeneratesatleast100values
*/ publicvoid testUnsizedLongsCountSeq() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 100;
r.longs().limit(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
/** *Asequentialunsizedstreamofdoublesgeneratesatleast100values
*/ publicvoid testUnsizedDoublesCountSeq() {
LongAdder counter = new LongAdder();
Random r = new Random(); long size = 100;
r.doubles().limit(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
/** *TestshufflingalistwithRandom.from()
*/ publicvoid testShufflingList() { finalvar listTest = new ArrayList<Integer>(); final RandomGenerator randomGenerator = RandomGenerator.getDefault(); final Random random = Random.from(randomGenerator);
for (int i = 0; i < 100; i++) {
listTest.add(i * 2);
} finalvar listCopy = new ArrayList<Integer>(listTest);
/* *TestwhethercallstomethodsinheritedfromRandomGenerator *aredelegatedtotheinstancereturnedbyfrom(). *Thisisnotacompletecoverage,butsimulatesthereproducer *inissueJDK-8288596
*/ publicvoid testRandomFrom() {
delegationCount = 0; var r = Random.from(new RandomGen());
r.isDeprecated();
r.nextFloat(1_000.0f);
r.nextFloat(); // not implemented in RandomGen, does not count
r.nextDouble(1_000.0);
r.nextDouble(); // not implemented in RandomGen, does not count
assertEquals(delegationCount, 3);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.