下面列出了怎么用java.util.SplittableRandom的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
/**
* 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);
}
}
}
/**
* 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);
}
}
/**
* 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);
}
}
}
/**
* 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);
}
}
/**
* 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);
}
}
}
@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();
}
}
/**
* 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);
}
}
/**
* 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);
}
}
}
@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();
}
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;
}
/**
* 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);
}
}
/**
* 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);
}
}
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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());
}
}
/**
* Invoking sized ints, long, doubles, with negative sizes throws
* IllegalArgumentException
*/
public void testBadStreamSize() {
SplittableRandom r = new SplittableRandom();
executeAndCatchIAE(() -> r.ints(-1L));
executeAndCatchIAE(() -> r.ints(-1L, 2, 3));
executeAndCatchIAE(() -> r.longs(-1L));
executeAndCatchIAE(() -> r.longs(-1L, -1L, 1L));
executeAndCatchIAE(() -> r.doubles(-1L));
executeAndCatchIAE(() -> r.doubles(-1L, .5, .6));
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
}
/**
* 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(size, counter.sum());
}
/**
* 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(fails.get(), 0);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
final BSBFDeDuplicator tempDeDuplicator = BSBFDeDuplicatorSerializers.VERSION_2.readFrom(in);
this.numBits = tempDeDuplicator.numBits;
this.numHashFunctions = tempDeDuplicator.numHashFunctions;
this.bloomFilters = tempDeDuplicator.bloomFilters;
this.hashBuffer = new int[this.bloomFilters.length];
this.random = new SplittableRandom(generateRandomSeed(this.numBits, this.numHashFunctions));
}
/**
* 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;
}
}
/**
* 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(fails.get(), 0);
}
/**
* 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;
}
}
Runner(Map<Integer,Integer> map,
Integer[] key,
CyclicBarrier barrier,
SplittableRandom rnd) {
this.map = map;
this.key = key;
this.barrier = barrier;
this.rnd = rnd;
position = key.length / 2;
}
static Integer[] makeKeys(int n) {
SplittableRandom rnd = new SplittableRandom();
Integer[] key = new Integer[n];
for (int i = 0; i < key.length; ++i)
key[i] = new Integer(rnd.nextInt());
return key;
}