Java源码示例:org.apache.commons.math.random.RandomDataImpl
示例1
/**
* Make sure that permuted arrays do not hash to the same value.
*/
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataImpl random = new RandomDataImpl();
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
original[i] = random.nextUniform(i + 0.5, i + 0.75);
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
示例2
/**
* Make sure that permuted arrays do not hash to the same value.
*/
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataImpl random = new RandomDataImpl();
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
original[i] = random.nextUniform(i + 0.5, i + 0.75);
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
示例3
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < primeList.length; i++) {
primes.add(Integer.valueOf(primeList[i]));
}
RandomDataImpl randomData = new RandomDataImpl();
for (int i = 0; i < 20; i++) {
Object[] sample = randomData.nextSample(primes, 4);
int p1 = ((Integer) sample[0]).intValue();
int p2 = ((Integer) sample[1]).intValue();
int p3 = ((Integer) sample[2]).intValue();
int p4 = ((Integer) sample[3]).intValue();
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;
assertEquals(gcd, MathUtils.gcd(i1, i2));
long l1 = i1;
long l2 = i2;
assertEquals(gcd, MathUtils.gcd(l1, l2));
}
}
示例4
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < primeList.length; i++) {
primes.add(Integer.valueOf(primeList[i]));
}
RandomDataImpl randomData = new RandomDataImpl();
for (int i = 0; i < 20; i++) {
Object[] sample = randomData.nextSample(primes, 4);
int p1 = ((Integer) sample[0]).intValue();
int p2 = ((Integer) sample[1]).intValue();
int p3 = ((Integer) sample[2]).intValue();
int p4 = ((Integer) sample[3]).intValue();
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;
assertEquals(gcd, MathUtils.gcd(i1, i2));
long l1 = i1;
long l2 = i2;
assertEquals(gcd, MathUtils.gcd(l1, l2));
}
}
示例5
/**
* Make sure that permuted arrays do not hash to the same value.
*/
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataImpl random = new RandomDataImpl();
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
original[i] = random.nextUniform(i + 0.5, i + 0.75);
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
示例6
/**
* Run nextInt range for 10000. Time:9, Heap:0.0M
0: 44.8%
1: 31.42%
2: 16.7%
3: 5.54%
4: 1.31%
5: 0.2%
6: 0.03%
7: 0.0%
8: 0.0%
9: 0.0%
* @throws Exception
*/
@Test
public void testNextInt() throws Exception {
int max = 10000;
final int[] ratio1 = new int[10];
final RandomData commonRandom = new RandomDataImpl();
TestUtil.doPerform(new Runnable() {
public void run() {
int r = MathUtil.nextGaussionInt(0, 10);
ratio1[r]++;
}
}, "nextInt range", max);
printRatio(ratio1, max);
}
示例7
@Test
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < primeList.length; i++) {
primes.add(Integer.valueOf(primeList[i]));
}
RandomDataImpl randomData = new RandomDataImpl();
for (int i = 0; i < 20; i++) {
Object[] sample = randomData.nextSample(primes, 4);
int p1 = ((Integer) sample[0]).intValue();
int p2 = ((Integer) sample[1]).intValue();
int p3 = ((Integer) sample[2]).intValue();
int p4 = ((Integer) sample[3]).intValue();
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;
Assert.assertEquals(gcd, MathUtils.gcd(i1, i2));
long l1 = i1;
long l2 = i2;
Assert.assertEquals(gcd, MathUtils.gcd(l1, l2));
}
}
示例8
@Test
public void testWithInitialCapacity() {
ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());
RandomData randomData = new RandomDataImpl();
int iterations = randomData.nextInt(100, 1000);
for( int i = 0; i < iterations; i++) {
eDA2.addElement( i );
}
Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());
eDA2.addElement( 2.0 );
Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
iterations + 1 , eDA2.getNumElements() );
}
示例9
@Test
public void testWithInitialCapacity() {
ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());
RandomData randomData = new RandomDataImpl();
int iterations = randomData.nextInt(100, 1000);
for( int i = 0; i < iterations; i++) {
eDA2.addElement( i );
}
Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());
eDA2.addElement( 2.0 );
Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
iterations + 1 , eDA2.getNumElements() );
}
示例10
/**
* Make sure that permuted arrays do not hash to the same value.
*/
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataImpl random = new RandomDataImpl();
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
original[i] = random.nextUniform(i + 0.5, i + 0.75);
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
示例11
/**
* Make sure that permuted arrays do not hash to the same value.
*/
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataImpl random = new RandomDataImpl();
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
original[i] = random.nextUniform(i + 0.5, i + 0.75);
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
示例12
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < primeList.length; i++) {
primes.add(Integer.valueOf(primeList[i]));
}
RandomDataImpl randomData = new RandomDataImpl();
for (int i = 0; i < 20; i++) {
Object[] sample = randomData.nextSample(primes, 4);
int p1 = ((Integer) sample[0]).intValue();
int p2 = ((Integer) sample[1]).intValue();
int p3 = ((Integer) sample[2]).intValue();
int p4 = ((Integer) sample[3]).intValue();
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;
assertEquals(gcd, MathUtils.gcd(i1, i2));
long l1 = i1;
long l2 = i2;
assertEquals(gcd, MathUtils.gcd(l1, l2));
}
}
示例13
@Test
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < primeList.length; i++) {
primes.add(Integer.valueOf(primeList[i]));
}
RandomDataImpl randomData = new RandomDataImpl();
for (int i = 0; i < 20; i++) {
Object[] sample = randomData.nextSample(primes, 4);
int p1 = ((Integer) sample[0]).intValue();
int p2 = ((Integer) sample[1]).intValue();
int p3 = ((Integer) sample[2]).intValue();
int p4 = ((Integer) sample[3]).intValue();
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;
Assert.assertEquals(gcd, MathUtils.gcd(i1, i2));
long l1 = i1;
long l2 = i2;
Assert.assertEquals(gcd, MathUtils.gcd(l1, l2));
}
}
示例14
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < primeList.length; i++) {
primes.add(Integer.valueOf(primeList[i]));
}
RandomDataImpl randomData = new RandomDataImpl();
for (int i = 0; i < 20; i++) {
Object[] sample = randomData.nextSample(primes, 4);
int p1 = ((Integer) sample[0]).intValue();
int p2 = ((Integer) sample[1]).intValue();
int p3 = ((Integer) sample[2]).intValue();
int p4 = ((Integer) sample[3]).intValue();
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;
assertEquals(gcd, MathUtils.gcd(i1, i2));
long l1 = i1;
long l2 = i2;
assertEquals(gcd, MathUtils.gcd(l1, l2));
}
}
示例15
/**
* Make sure that permuted arrays do not hash to the same value.
*/
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataImpl random = new RandomDataImpl();
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
original[i] = random.nextUniform(i + 0.5, i + 0.75);
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
示例16
/**
* Create a NaturalRanking with the given TiesStrategy.
*
* @param tiesStrategy the TiesStrategy to use
*/
public NaturalRanking(TiesStrategy tiesStrategy) {
super();
this.tiesStrategy = tiesStrategy;
nanStrategy = DEFAULT_NAN_STRATEGY;
randomData = new RandomDataImpl();
}
示例17
/**
* Create a NaturalRanking with the given TiesStrategy.
*
* @param tiesStrategy the TiesStrategy to use
*/
public NaturalRanking(TiesStrategy tiesStrategy) {
super();
this.tiesStrategy = tiesStrategy;
nanStrategy = DEFAULT_NAN_STRATEGY;
randomData = new RandomDataImpl();
}
示例18
/**
* Create a NaturalRanking with the given NaNStrategy and TiesStrategy.
*
* @param nanStrategy NaNStrategy to use
* @param tiesStrategy TiesStrategy to use
*/
public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
super();
this.nanStrategy = nanStrategy;
this.tiesStrategy = tiesStrategy;
randomData = new RandomDataImpl();
}
示例19
/**
* Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM
* and the given source of random data.
*
* @param nanStrategy NaNStrategy to use
* @param randomGenerator source of random data
*/
public NaturalRanking(NaNStrategy nanStrategy,
RandomGenerator randomGenerator) {
super();
this.nanStrategy = nanStrategy;
this.tiesStrategy = TiesStrategy.RANDOM;
randomData = new RandomDataImpl(randomGenerator);
}
示例20
/**
* Generates a partition of <sample> into up to 5 sequentially selected
* subsamples with randomly selected partition points.
*
* @param sample array to partition
* @return rectangular array with rows = subsamples
*/
private double[][] generatePartition(double[] sample) {
final int length = sample.length;
final double[][] out = new double[5][];
final RandomData randomData = new RandomDataImpl();
int cur = 0;
int offset = 0;
int sampleCount = 0;
for (int i = 0; i < 5; i++) {
if (cur == length || offset == length) {
break;
}
final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
final int subLength = next - cur + 1;
out[i] = new double[subLength];
System.arraycopy(sample, offset, out[i], 0, subLength);
cur = next + 1;
sampleCount++;
offset += subLength;
}
if (sampleCount < 5) {
double[][] out2 = new double[sampleCount][];
for (int j = 0; j < sampleCount; j++) {
final int curSize = out[j].length;
out2[j] = new double[curSize];
System.arraycopy(out[j], 0, out2[j], 0, curSize);
}
return out2;
} else {
return out;
}
}
示例21
/**
* Generates a random sample of double values.
* Sample size is random, between 10 and 100 and values are
* uniformly distributed over [-100, 100].
*
* @return array of random double values
*/
private double[] generateSample() {
final RandomData randomData = new RandomDataImpl();
final int sampleSize = randomData.nextInt(10,100);
double[] out = new double[sampleSize];
for (int i = 0; i < out.length; i++) {
out[i] = randomData.nextUniform(-100, 100);
}
return out;
}
示例22
/**
* Create a NaturalRanking with the given TiesStrategy.
*
* @param tiesStrategy the TiesStrategy to use
*/
public NaturalRanking(TiesStrategy tiesStrategy) {
super();
this.tiesStrategy = tiesStrategy;
nanStrategy = DEFAULT_NAN_STRATEGY;
randomData = new RandomDataImpl();
}
示例23
/**
* Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM
* and the given source of random data.
*
* @param nanStrategy NaNStrategy to use
* @param randomGenerator source of random data
*/
public NaturalRanking(NaNStrategy nanStrategy,
RandomGenerator randomGenerator) {
super();
this.nanStrategy = nanStrategy;
this.tiesStrategy = TiesStrategy.RANDOM;
randomData = new RandomDataImpl(randomGenerator);
}
示例24
/**
* Create a NaturalRanking with the given TiesStrategy.
*
* @param tiesStrategy the TiesStrategy to use
*/
public NaturalRanking(TiesStrategy tiesStrategy) {
super();
this.tiesStrategy = tiesStrategy;
nanStrategy = DEFAULT_NAN_STRATEGY;
randomData = new RandomDataImpl();
}
示例25
public void testGetSortedValues() {
double[] test1 = {5,4,3,2,1};
double[] test2 = {5,2,1,3,4,0};
double[] test3 = {1};
int[] testi = null;
double[] test4 = null;
RandomData rd = new RandomDataImpl();
tstGetSortedValues(test1);
tstGetSortedValues(test2);
tstGetSortedValues(test3);
for (int i = 0; i < 10; i++) {
testi = rd.nextPermutation(10,6);
test4 = new double[6];
for (int j = 0; j < testi.length; j++) {
test4[j] = (double) testi[j];
}
tstGetSortedValues(test4);
}
for (int i = 0; i < 10; i++) {
testi = rd.nextPermutation(10,5);
test4 = new double[5];
for (int j = 0; j < testi.length; j++) {
test4[j] = (double) testi[j];
}
tstGetSortedValues(test4);
}
}
示例26
/**
* Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM
* and the given source of random data.
*
* @param nanStrategy NaNStrategy to use
* @param randomGenerator source of random data
*/
public NaturalRanking(NaNStrategy nanStrategy,
RandomGenerator randomGenerator) {
super();
this.nanStrategy = nanStrategy;
this.tiesStrategy = TiesStrategy.RANDOM;
randomData = new RandomDataImpl(randomGenerator);
}
示例27
/**
* Generates a partition of <sample> into up to 5 sequentially selected
* subsamples with randomly selected partition points.
*
* @param sample array to partition
* @return rectangular array with rows = subsamples
*/
private double[][] generatePartition(double[] sample) {
final int length = sample.length;
final double[][] out = new double[5][];
final RandomData randomData = new RandomDataImpl();
int cur = 0;
int offset = 0;
int sampleCount = 0;
for (int i = 0; i < 5; i++) {
if (cur == length || offset == length) {
break;
}
final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
final int subLength = next - cur + 1;
out[i] = new double[subLength];
System.arraycopy(sample, offset, out[i], 0, subLength);
cur = next + 1;
sampleCount++;
offset += subLength;
}
if (sampleCount < 5) {
double[][] out2 = new double[sampleCount][];
for (int j = 0; j < sampleCount; j++) {
final int curSize = out[j].length;
out2[j] = new double[curSize];
System.arraycopy(out[j], 0, out2[j], 0, curSize);
}
return out2;
} else {
return out;
}
}
示例28
/**
* Generates a random sample of double values.
* Sample size is random, between 10 and 100 and values are
* uniformly distributed over [-100, 100].
*
* @return array of random double values
*/
private double[] generateSample() {
final RandomData randomData = new RandomDataImpl();
final int sampleSize = randomData.nextInt(10,100);
double[] out = new double[sampleSize];
for (int i = 0; i < out.length; i++) {
out[i] = randomData.nextUniform(-100, 100);
}
return out;
}
示例29
/**
* Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM
* and the given source of random data.
*
* @param nanStrategy NaNStrategy to use
* @param randomGenerator source of random data
*/
public NaturalRanking(NaNStrategy nanStrategy,
RandomGenerator randomGenerator) {
super();
this.nanStrategy = nanStrategy;
this.tiesStrategy = TiesStrategy.RANDOM;
randomData = new RandomDataImpl(randomGenerator);
}
示例30
/**
* Create a NaturalRanking with the given NaNStrategy and TiesStrategy.
*
* @param nanStrategy NaNStrategy to use
* @param tiesStrategy TiesStrategy to use
*/
public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
super();
this.nanStrategy = nanStrategy;
this.tiesStrategy = tiesStrategy;
randomData = new RandomDataImpl();
}