This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit 2218194942ea095fd15b3bc1552e12a263981f80 Author: Gilles Sadowski <gillese...@gmail.com> AuthorDate: Sat Jul 17 14:19:09 2021 +0200 MATH-1611: Obsolete class. Functionality moved to "Commons RNG" (cf. RNG-140). --- .../commons/math4/legacy/random/RandomUtils.java | 94 --------------- .../RandomUtilsDataGeneratorAbstractTest.java | 128 --------------------- 2 files changed, 222 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/RandomUtils.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/RandomUtils.java deleted file mode 100644 index 775693c..0000000 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/RandomUtils.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.math4.legacy.random; - -import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException; -import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; -import org.apache.commons.rng.UniformRandomProvider; - -/** - * Factory for creating generators of miscellaneous data. - * - * @since 4.0 - */ -public final class RandomUtils { - /** - * Class contains only static methods. - */ - private RandomUtils() {} - - /** - * @param rng Underlying generator. Reference is copied so the RNG - * is shared with the caller. - * @return a {@link DataGenerator data generator}. - */ - public static DataGenerator createDataGenerator(final UniformRandomProvider rng) { - return new DataGenerator(rng); - } - - /** - * Various random data generation routines. - */ - public static class DataGenerator { - /** Underlying RNG. */ - private final UniformRandomProvider rng; - - /** - * @param rng Underlying generator. - */ - DataGenerator(UniformRandomProvider rng) { - this.rng = rng; - } - - /** - * Generates a uniformly distributed random long integer between {@code lower} - * and {@code upper} (endpoints included). - * - * @param lower Lower bound for generated long integer. - * @param upper Upper bound for generated long integer. - * @return a random long integer greater than or equal to {@code lower} - * and less than or equal to {@code upper} - * @throws NumberIsTooLargeException if {@code lower >= upper} - */ - public long nextLong(final long lower, - final long upper) { - if (lower >= upper) { - throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, - lower, upper, false); - } - final long max = (upper - lower) + 1; - if (max <= 0) { - // Range is too wide to fit in a positive long (larger than 2^63); - // as it covers more than half the long range, we use directly a - // simple rejection method. - while (true) { - final long r = rng.nextLong(); - if (r >= lower && r <= upper) { - return r; - } - } - } else if (max < Integer.MAX_VALUE){ - // We can shift the range and generate directly a positive int. - return lower + rng.nextInt((int) max); - } else { - // We can shift the range and generate directly a positive long. - return lower + rng.nextLong(max); - } - } - } -} diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/RandomUtilsDataGeneratorAbstractTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/RandomUtilsDataGeneratorAbstractTest.java deleted file mode 100644 index c1f611a..0000000 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/RandomUtilsDataGeneratorAbstractTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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.math4.legacy.random; - -import org.apache.commons.math4.legacy.RetryRunner; -import org.apache.commons.math4.legacy.TestUtils; -import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; -import org.apache.commons.math4.legacy.stat.Frequency; -import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath; -import org.apache.commons.rng.UniformRandomProvider; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * Test cases for the {@link RandomUtils.DataGenerator} class. - */ -@RunWith(RetryRunner.class) -public abstract class RandomUtilsDataGeneratorAbstractTest { - private final long smallSampleSize = 1000; - private final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", - "a", "b", "c", "d", "e", "f" }; - /** Data generator. */ - private final RandomUtils.DataGenerator randomData; - - /** - * @param rng RNG. - */ - protected RandomUtilsDataGeneratorAbstractTest(UniformRandomProvider rng) { - randomData = RandomUtils.createDataGenerator(rng); - } - - @Test - public void testNextLongExtremeValues() { - long x = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); - long y = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); - Assert.assertFalse(x == y); - } - - @Test - public void testNextLongIAE() { - try { - randomData.nextLong(4, 3); - Assert.fail("MathIllegalArgumentException expected"); - } catch (MathIllegalArgumentException ex) { - // ignored - } - } - - @Test - public void testNextLongNegativeToPositiveRange() { - for (int i = 0; i < 5; i++) { - checkNextLongUniform(-3, 5); - checkNextLongUniform(-3, 6); - } - } - - @Test - public void testNextLongNegativeRange() { - for (int i = 0; i < 5; i++) { - checkNextLongUniform(-7, -4); - checkNextLongUniform(-15, -2); - checkNextLongUniform(Long.MIN_VALUE + 1, Long.MIN_VALUE + 12); - } - } - - @Test - public void testNextLongPositiveRange() { - for (int i = 0; i < 5; i++) { - checkNextLongUniform(0, 3); - checkNextLongUniform(2, 12); - checkNextLongUniform(Long.MAX_VALUE - 12, Long.MAX_VALUE - 1); - } - } - - private void checkNextLongUniform(long min, long max) { - final Frequency<Long> freq = new Frequency<>(); - for (int i = 0; i < smallSampleSize; i++) { - final long value = randomData.nextLong(min, max); - Assert.assertTrue("nextLong range: " + value + " " + min + " " + max, - (value >= min) && (value <= max)); - freq.addValue(value); - } - final int len = ((int) (max - min)) + 1; - final long[] observed = new long[len]; - for (int i = 0; i < len; i++) { - observed[i] = freq.getCount(min + i); - } - final double[] expected = new double[len]; - for (int i = 0; i < len; i++) { - expected[i] = 1d / len; - } - - TestUtils.assertChiSquareAccept(expected, observed, 0.01); - } - - @Test - public void testNextLongWideRange() { - long lower = -0x6543210FEDCBA987L; - long upper = 0x456789ABCDEF0123L; - long max = Long.MIN_VALUE; - long min = Long.MAX_VALUE; - for (int i = 0; i < 10000000; ++i) { - long r = randomData.nextLong(lower, upper); - max = AccurateMath.max(max, r); - min = AccurateMath.min(min, r); - Assert.assertTrue(r >= lower); - Assert.assertTrue(r <= upper); - } - double ratio = (((double) max) - ((double) min)) / - (((double) upper) - ((double) lower)); - Assert.assertTrue(ratio > 0.99999); - } -}