mirror of
https://github.com/apache/commons-math.git
synced 2025-02-08 11:05:56 +00:00
Refactored RandomGenerator tests.
* Added RandomGeneratorAbstractTest collecting stock tests for RandomGenerator implementations and running RandomDataTest test cases using RandomDataImpls constructed from generators under test. * Added BitsStreamGeneratorTest extending RandomGeneratorAbstractTest to test abstract method implementations in BitStreamGenerator. * Changed Mersenne and Well generator tests to extend RandomGeneratorAbstractTest. * Improved test coverage (discovering MATH-640) of AbstractRandomGeneratorTest by making this class extend RandomGeneratorAbstractTest. All new tests use fixed seeds. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1153257 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d2d9d32f79
commit
b468defc67
@ -16,143 +16,32 @@
|
||||
*/
|
||||
package org.apache.commons.math.random;
|
||||
|
||||
import org.apache.commons.math.stat.Frequency;
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math.Retry;
|
||||
import org.apache.commons.math.RetryRunner;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test cases for the AbstractRandomGenerator class
|
||||
* Test cases for the AbstractRandomGenerator class.
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
@RunWith(RetryRunner.class)
|
||||
public class AbstractRandomGeneratorTest extends RandomDataTest {
|
||||
|
||||
protected TestRandomGenerator testGenerator = new TestRandomGenerator();
|
||||
public class AbstractRandomGeneratorTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Override
|
||||
public void testNextInt2() {
|
||||
// Currently broken. Remove this stub when MATH-640 is resolved
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testNextLong2() {
|
||||
// Currently broken. Remove this stub when MATH-640 is resolved
|
||||
}
|
||||
|
||||
public AbstractRandomGeneratorTest() {
|
||||
randomData = new RandomDataImpl(testGenerator);
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
@Retry(2)
|
||||
public void testNextInt() {
|
||||
try {
|
||||
testGenerator.nextInt(-1);
|
||||
Assert.fail("MathIllegalArgumentException expected");
|
||||
} catch (MathIllegalArgumentException ex) {
|
||||
// ignored
|
||||
}
|
||||
Frequency freq = new Frequency();
|
||||
int value = 0;
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
value = testGenerator.nextInt(4);
|
||||
Assert.assertTrue("nextInt range",(value >= 0) && (value <= 3));
|
||||
freq.addValue(value);
|
||||
}
|
||||
long[] observed = new long[4];
|
||||
for (int i=0; i<4; i++) {
|
||||
observed[i] = freq.getCount(i);
|
||||
}
|
||||
|
||||
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
||||
* Change to 11.34 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 16.27);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
@Retry(2)
|
||||
public void testNextLong() {
|
||||
long q1 = Long.MAX_VALUE/4;
|
||||
long q2 = 2 * q1;
|
||||
long q3 = 3 * q1;
|
||||
|
||||
Frequency freq = new Frequency();
|
||||
long val = 0;
|
||||
int value = 0;
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
val = testGenerator.nextLong();
|
||||
if (val < q1) {
|
||||
value = 0;
|
||||
} else if (val < q2) {
|
||||
value = 1;
|
||||
} else if (val < q3) {
|
||||
value = 2;
|
||||
} else {
|
||||
value = 3;
|
||||
}
|
||||
freq.addValue(value);
|
||||
}
|
||||
long[] observed = new long[4];
|
||||
for (int i=0; i<4; i++) {
|
||||
observed[i] = freq.getCount(i);
|
||||
}
|
||||
|
||||
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
||||
* Change to 11.34 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 16.27);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Retry(2)
|
||||
public void testNextBoolean() {
|
||||
long halfSampleSize = smallSampleSize / 2;
|
||||
double[] expected = {halfSampleSize, halfSampleSize};
|
||||
long[] observed = new long[2];
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
if (testGenerator.nextBoolean()) {
|
||||
observed[0]++;
|
||||
} else {
|
||||
observed[1]++;
|
||||
}
|
||||
}
|
||||
/* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
|
||||
* Change to 6.635 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 10.828);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Retry(2)
|
||||
public void testNextFloat() {
|
||||
Frequency freq = new Frequency();
|
||||
float val = 0;
|
||||
int value = 0;
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
val = testGenerator.nextFloat();
|
||||
if (val < 0.25) {
|
||||
value = 0;
|
||||
} else if (val < 0.5) {
|
||||
value = 1;
|
||||
} else if (val < 0.75) {
|
||||
value = 2;
|
||||
} else {
|
||||
value = 3;
|
||||
}
|
||||
freq.addValue(value);
|
||||
}
|
||||
long[] observed = new long[4];
|
||||
for (int i=0; i<4; i++) {
|
||||
observed[i] = freq.getCount(i);
|
||||
}
|
||||
|
||||
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
||||
* Change to 11.34 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 16.27);
|
||||
|
||||
protected RandomGenerator makeGenerator() {
|
||||
RandomGenerator generator = new TestRandomGenerator();
|
||||
generator.setSeed(1000);
|
||||
return generator;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.math.random;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Test cases for the BitStreamGenerator class
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
public class BitsStreamGeneratorTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
public BitsStreamGeneratorTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
protected RandomGenerator makeGenerator() {
|
||||
RandomGenerator generator = new TestBitStreamGenerator();
|
||||
generator.setSeed(1000);
|
||||
return generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test BitStreamGenerator using a Random as bit source.
|
||||
*/
|
||||
static class TestBitStreamGenerator extends BitsStreamGenerator {
|
||||
|
||||
private BitRandom ran = new BitRandom();
|
||||
|
||||
@Override
|
||||
public void setSeed(int seed) {
|
||||
ran.setSeed(seed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeed(int[] seed) {
|
||||
ran.setSeed(seed[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeed(long seed) {
|
||||
ran.setSeed((int) seed);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int next(int bits) {
|
||||
return ran.nextBits(bits);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend Random to expose next(bits)
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
static class BitRandom extends Random {
|
||||
public BitRandom() {
|
||||
super();
|
||||
}
|
||||
public int nextBits(int bits) {
|
||||
return next(bits);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -16,140 +16,19 @@
|
||||
*/
|
||||
package org.apache.commons.math.random;
|
||||
|
||||
|
||||
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MersenneTwisterTest {
|
||||
public class MersenneTwisterTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Test
|
||||
public void testGaussian() {
|
||||
MersenneTwister mt = new MersenneTwister(42853252100l);
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
sample.addValue(mt.nextGaussian());
|
||||
}
|
||||
Assert.assertEquals(0.0, sample.getMean(), 0.005);
|
||||
Assert.assertEquals(1.0, sample.getStandardDeviation(), 0.025);
|
||||
@Override
|
||||
protected RandomGenerator makeGenerator() {
|
||||
return new MersenneTwister(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() {
|
||||
MersenneTwister mt = new MersenneTwister(195357343514l);
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
sample.addValue(mt.nextDouble());
|
||||
}
|
||||
Assert.assertEquals(0.5, sample.getMean(), 0.02);
|
||||
Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
|
||||
sample.getStandardDeviation(),
|
||||
0.002);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() {
|
||||
MersenneTwister mt = new MersenneTwister(4442733263l);
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
sample.addValue(mt.nextFloat());
|
||||
}
|
||||
Assert.assertEquals(0.5, sample.getMean(), 0.01);
|
||||
Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
|
||||
sample.getStandardDeviation(),
|
||||
0.006);
|
||||
}
|
||||
|
||||
@Test(expected=MathIllegalArgumentException.class)
|
||||
public void testNextIntNeg() {
|
||||
new MersenneTwister(1).nextInt(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextIntN() {
|
||||
MersenneTwister mt = new MersenneTwister(0x12b8a7412bb25el);
|
||||
for (int n = 1; n < 20; ++n) {
|
||||
int[] count = new int[n];
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
int l = mt.nextInt(n);
|
||||
++count[l];
|
||||
Assert.assertTrue(l >= 0);
|
||||
Assert.assertTrue(l < n);
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Assert.assertTrue(n * count[i] > 8600);
|
||||
Assert.assertTrue(n * count[i] < 11200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextInt() {
|
||||
MersenneTwister mt = new MersenneTwister(new int[] { 1, 2, 3, 4, 5 });
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (mt.nextInt() >= 0) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 120);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextLong() {
|
||||
MersenneTwister mt = new MersenneTwister(12345);
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (mt.nextLong() >= 0) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNexBoolean() {
|
||||
MersenneTwister mt = new MersenneTwister(76342);
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (mt.nextBoolean()) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 250);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNexBytes() {
|
||||
MersenneTwister mt = new MersenneTwister(0);
|
||||
int[] count = new int[256];
|
||||
byte[] bytes = new byte[10];
|
||||
for (int k = 0; k < 100000; ++k) {
|
||||
mt.nextBytes(bytes);
|
||||
for (byte b : bytes) {
|
||||
++count[b + 128];
|
||||
}
|
||||
}
|
||||
int min = Integer.MAX_VALUE;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int c : count) {
|
||||
min = FastMath.min(min, c);
|
||||
max = FastMath.max(max, c);
|
||||
}
|
||||
int expected = (100000 * bytes.length) / count.length;
|
||||
Assert.assertTrue((expected - 200) < min);
|
||||
Assert.assertTrue(max < (expected + 200));
|
||||
}
|
||||
|
||||
|
||||
// TODO: Some of the tests moved up to RandomGeneratorAbstractTest tested alternative seeding / constructors
|
||||
// Tests exercising these features directly should be added to this class.
|
||||
|
||||
@Test
|
||||
public void testMakotoNishimura() {
|
||||
MersenneTwister mt = new MersenneTwister(new int[] {0x123, 0x234, 0x345, 0x456});
|
||||
|
@ -65,6 +65,7 @@ public class RandomDataTest {
|
||||
|
||||
public RandomDataTest() {
|
||||
randomData = new RandomDataImpl();
|
||||
randomData.reSeed(1000);
|
||||
}
|
||||
|
||||
protected final long smallSampleSize = 1000;
|
||||
@ -247,9 +248,6 @@ public class RandomDataTest {
|
||||
@Test
|
||||
public void testNextPoissonConsistency() throws Exception {
|
||||
|
||||
// Reseed randomGenerator to get fixed sequence
|
||||
randomData.reSeed(1000);
|
||||
|
||||
// Small integral means
|
||||
for (int i = 1; i < 100; i++) {
|
||||
checkNextPoissonConsistency(i);
|
||||
|
@ -0,0 +1,280 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.math.random;
|
||||
|
||||
import org.apache.commons.math.TestUtils;
|
||||
import org.apache.commons.math.stat.Frequency;
|
||||
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Base class for RandomGenerator tests.
|
||||
*
|
||||
* Tests RandomGenerator methods directly and also executes RandomDataTest
|
||||
* test cases against a RandomDataImpl created using the provided generator.
|
||||
*
|
||||
* RandomGenerator test classes should extend this class, implementing
|
||||
* makeGenerator() to provide a concrete generator to test. The generator
|
||||
* returned by makeGenerator should be seeded with a fixed seed.
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
public abstract class RandomGeneratorAbstractTest extends RandomDataTest {
|
||||
|
||||
/** RandomGenerator under test */
|
||||
protected RandomGenerator generator;
|
||||
|
||||
/**
|
||||
* Override this method in subclasses to provide a concrete generator to test.
|
||||
* Return a generator seeded with a fixed seed.
|
||||
*/
|
||||
protected abstract RandomGenerator makeGenerator();
|
||||
|
||||
/**
|
||||
* Initialize generator and randomData instance in superclass.
|
||||
*/
|
||||
public RandomGeneratorAbstractTest() {
|
||||
generator = makeGenerator();
|
||||
randomData = new RandomDataImpl(generator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a fixed seed for the tests
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
generator = makeGenerator();
|
||||
}
|
||||
|
||||
// Omit secureXxx tests, since they do not use the provided generator
|
||||
@Override
|
||||
public void testNextSecureLong() {}
|
||||
|
||||
@Override
|
||||
public void testNextSecureInt() {}
|
||||
|
||||
@Override
|
||||
public void testNextSecureHex() {}
|
||||
|
||||
@Test
|
||||
public void testNextIntDirect() {
|
||||
try {
|
||||
generator.nextInt(-1);
|
||||
Assert.fail("MathIllegalArgumentException expected");
|
||||
} catch (MathIllegalArgumentException ex) {
|
||||
// ignored
|
||||
}
|
||||
Frequency freq = new Frequency();
|
||||
int value = 0;
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
value = generator.nextInt(4);
|
||||
Assert.assertTrue("nextInt range",(value >= 0) && (value <= 3));
|
||||
freq.addValue(value);
|
||||
}
|
||||
long[] observed = new long[4];
|
||||
for (int i=0; i<4; i++) {
|
||||
observed[i] = freq.getCount(i);
|
||||
}
|
||||
|
||||
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
||||
* Change to 11.34 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 16.27);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextLongDirect() {
|
||||
long q1 = Long.MAX_VALUE/4;
|
||||
long q2 = 2 * q1;
|
||||
long q3 = 3 * q1;
|
||||
|
||||
Frequency freq = new Frequency();
|
||||
long val = 0;
|
||||
int value = 0;
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
val = generator.nextLong();
|
||||
val = val < 0 ? -val : val;
|
||||
if (val < q1) {
|
||||
value = 0;
|
||||
} else if (val < q2) {
|
||||
value = 1;
|
||||
} else if (val < q3) {
|
||||
value = 2;
|
||||
} else {
|
||||
value = 3;
|
||||
}
|
||||
freq.addValue(value);
|
||||
}
|
||||
long[] observed = new long[4];
|
||||
for (int i=0; i<4; i++) {
|
||||
observed[i] = freq.getCount(i);
|
||||
}
|
||||
|
||||
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
||||
* Change to 11.34 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 16.27);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextBooleanDirect() {
|
||||
long halfSampleSize = smallSampleSize / 2;
|
||||
double[] expected = {halfSampleSize, halfSampleSize};
|
||||
long[] observed = new long[2];
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
if (generator.nextBoolean()) {
|
||||
observed[0]++;
|
||||
} else {
|
||||
observed[1]++;
|
||||
}
|
||||
}
|
||||
/* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
|
||||
* Change to 6.635 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 10.828);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextFloatDirect() {
|
||||
Frequency freq = new Frequency();
|
||||
float val = 0;
|
||||
int value = 0;
|
||||
for (int i=0; i<smallSampleSize; i++) {
|
||||
val = generator.nextFloat();
|
||||
if (val < 0.25) {
|
||||
value = 0;
|
||||
} else if (val < 0.5) {
|
||||
value = 1;
|
||||
} else if (val < 0.75) {
|
||||
value = 2;
|
||||
} else {
|
||||
value = 3;
|
||||
}
|
||||
freq.addValue(value);
|
||||
}
|
||||
long[] observed = new long[4];
|
||||
for (int i=0; i<4; i++) {
|
||||
observed[i] = freq.getCount(i);
|
||||
}
|
||||
|
||||
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
||||
* Change to 11.34 for alpha = .01
|
||||
*/
|
||||
Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
|
||||
testStatistic.chiSquare(expected,observed) < 16.27);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleDirect() {
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
sample.addValue(generator.nextDouble());
|
||||
}
|
||||
Assert.assertEquals(0.5, sample.getMean(), 0.02);
|
||||
Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
|
||||
sample.getStandardDeviation(),
|
||||
0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatDirect() {
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
sample.addValue(generator.nextFloat());
|
||||
}
|
||||
Assert.assertEquals(0.5, sample.getMean(), 0.01);
|
||||
Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
|
||||
sample.getStandardDeviation(),
|
||||
0.01);
|
||||
}
|
||||
|
||||
@Test(expected=MathIllegalArgumentException.class)
|
||||
public void testNextIntNeg() {
|
||||
generator.nextInt(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextInt2() {
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (generator.nextInt() >= 0) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue("Walked too far astray: " + walk, FastMath.abs(walk) < 120);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextLong2() {
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 1000; ++k) {
|
||||
if (generator.nextLong() >= 0) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue("Walked too far astray: " + walk, FastMath.abs(walk) < 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNexBoolean2() {
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (generator.nextBoolean()) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 250);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNexBytes() throws Exception {
|
||||
long[] count = new long[256];
|
||||
byte[] bytes = new byte[10];
|
||||
double[] expected = new double[256];
|
||||
final int sampleSize = 100000;
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
expected[i] = (double) sampleSize / 265f;
|
||||
}
|
||||
|
||||
for (int k = 0; k < sampleSize; ++k) {
|
||||
generator.nextBytes(bytes);
|
||||
for (byte b : bytes) {
|
||||
++count[b + 128];
|
||||
}
|
||||
}
|
||||
|
||||
TestUtils.assertChiSquareAccept(expected, count, 0.001);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -18,137 +18,15 @@ package org.apache.commons.math.random;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Well1024aTest {
|
||||
public class Well1024aTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Test
|
||||
public void testGaussian() {
|
||||
Well1024a mt = new Well1024a(42853252100l);
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
sample.addValue(mt.nextGaussian());
|
||||
}
|
||||
Assert.assertEquals(0.0, sample.getMean(), 0.004);
|
||||
Assert.assertEquals(1.0, sample.getStandardDeviation(), 0.003);
|
||||
@Override
|
||||
protected RandomGenerator makeGenerator() {
|
||||
return new Well1024a(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() {
|
||||
Well1024a mt = new Well1024a(195357343514l);
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
sample.addValue(mt.nextDouble());
|
||||
}
|
||||
Assert.assertEquals(0.5, sample.getMean(), 0.0006);
|
||||
Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
|
||||
sample.getStandardDeviation(),
|
||||
0.002);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() {
|
||||
Well1024a mt = new Well1024a(4442733263l);
|
||||
SummaryStatistics sample = new SummaryStatistics();
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
sample.addValue(mt.nextFloat());
|
||||
}
|
||||
Assert.assertEquals(0.5, sample.getMean(), 0.0001);
|
||||
Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
|
||||
sample.getStandardDeviation(),
|
||||
0.003);
|
||||
}
|
||||
|
||||
@Test(expected=MathIllegalArgumentException.class)
|
||||
public void testNextIntNeg() {
|
||||
new Well1024a(1).nextInt(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextIntN() {
|
||||
Well1024a mt = new Well1024a(0x12b8a7412bb25el);
|
||||
for (int n = 1; n < 20; ++n) {
|
||||
int[] count = new int[n];
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
int l = mt.nextInt(n);
|
||||
++count[l];
|
||||
Assert.assertTrue(l >= 0);
|
||||
Assert.assertTrue(l < n);
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Assert.assertTrue(n * count[i] > 8600);
|
||||
Assert.assertTrue(n * count[i] < 11200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextInt() {
|
||||
Well1024a mt = new Well1024a(new int[] { 1, 2, 3, 4, 5 });
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (mt.nextInt() >= 0) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 70);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextLong() {
|
||||
Well1024a mt = new Well1024a(12345);
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (mt.nextLong() >= 0) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 70);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNexBoolean() {
|
||||
Well1024a mt = new Well1024a(76342);
|
||||
int walk = 0;
|
||||
for (int k = 0; k < 10000; ++k) {
|
||||
if (mt.nextBoolean()) {
|
||||
++walk;
|
||||
} else {
|
||||
--walk;
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(FastMath.abs(walk) < 180);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNexBytes() {
|
||||
Well1024a mt = new Well1024a(0);
|
||||
int[] count = new int[256];
|
||||
byte[] bytes = new byte[10];
|
||||
for (int k = 0; k < 1000000; ++k) {
|
||||
mt.nextBytes(bytes);
|
||||
for (byte b : bytes) {
|
||||
++count[b + 128];
|
||||
}
|
||||
}
|
||||
int min = Integer.MAX_VALUE;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int c : count) {
|
||||
min = FastMath.min(min, c);
|
||||
max = FastMath.max(max, c);
|
||||
}
|
||||
int expected = (1000000 * bytes.length) / count.length;
|
||||
Assert.assertTrue((expected - 600) < min);
|
||||
Assert.assertTrue(max < (expected + 600));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testReferenceCode() {
|
||||
Well1024a mt = new Well1024a(new int[] {
|
||||
|
@ -19,7 +19,12 @@ package org.apache.commons.math.random;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Well19937aTest {
|
||||
public class Well19937aTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Override
|
||||
public RandomGenerator makeGenerator() {
|
||||
return new Well19937a(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReferenceCode() {
|
||||
|
@ -19,7 +19,12 @@ package org.apache.commons.math.random;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Well19937cTest {
|
||||
public class Well19937cTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Override
|
||||
public RandomGenerator makeGenerator() {
|
||||
return new Well19937c(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReferenceCode() {
|
||||
|
@ -19,8 +19,13 @@ package org.apache.commons.math.random;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Well44497aTest {
|
||||
|
||||
public class Well44497aTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Override
|
||||
public RandomGenerator makeGenerator() {
|
||||
return new Well44497a(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReferenceCode() {
|
||||
int[] base = {
|
||||
|
@ -19,8 +19,13 @@ package org.apache.commons.math.random;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Well44497bTest {
|
||||
|
||||
public class Well44497bTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Override
|
||||
public RandomGenerator makeGenerator() {
|
||||
return new Well44497b(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReferenceCode() {
|
||||
int[] base = {
|
||||
|
@ -19,8 +19,12 @@ package org.apache.commons.math.random;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Well512aTest {
|
||||
|
||||
public class Well512aTest extends RandomGeneratorAbstractTest {
|
||||
|
||||
@Override
|
||||
public RandomGenerator makeGenerator() {
|
||||
return new Well512a(100);
|
||||
}
|
||||
@Test
|
||||
public void testReferenceCode() {
|
||||
Well512a mt = new Well512a(new int[] {
|
||||
|
Loading…
x
Reference in New Issue
Block a user