MATH-1335

Use new RNG API.

Unit tests rewritten to avoid unnecessary usage of "RandomDataGenerator" (cf. MATH-1341).
This commit is contained in:
Gilles 2016-05-11 17:26:37 +02:00
parent 11a7e62a89
commit 0484bdb3f6
2 changed files with 14 additions and 19 deletions

View File

@ -22,8 +22,6 @@ import java.math.BigInteger;
import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.random.RandomDataGenerator;
import org.apache.commons.math4.util.ArithmeticUtils;
import org.junit.Assert;
import org.junit.Test;
@ -125,17 +123,13 @@ public class ArithmeticUtilsTest {
@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]));
}
RandomDataGenerator randomData = new RandomDataGenerator();
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();
MathArrays.shuffle(primeList);
int p1 = primeList[0];
int p2 = primeList[1];
int p3 = primeList[2];
int p4 = primeList[3];
int i1 = p1 * p2 * p3;
int i2 = p1 * p2 * p4;
int gcd = p1 * p2;

View File

@ -19,10 +19,8 @@ import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.NotFiniteNumberException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
import org.apache.commons.math4.random.RandomDataGenerator;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathUtils;
import org.junit.Assert;
import org.junit.Test;
@ -97,23 +95,26 @@ public final class MathUtilsTest {
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
RandomDataGenerator random = new RandomDataGenerator();
final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_512_A,
64925784252L);
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
final RealDistribution.Sampler u
= new UniformRealDistribution(i + 0.5, i + 0.75).createSampler(RandomSource.create(RandomSource.WELL_512_A,
64925784252L));
= new UniformRealDistribution(i + 0.5, i + 0.75).createSampler(random);
original[i] = u.sample();
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = random.nextPermutation(10, 10);
int[] permutation = MathArrays.natural(10);
MathArrays.shuffle(permutation, random);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
break;
}
permuted[i] = original[permutation[i]];
}