This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
The following commit(s) were added to refs/heads/master by this push:
new 6c75e8bc RNG-197: Drop methods not implemented by java.util.Random
6c75e8bc is described below
commit 6c75e8bcb0cd058ee5cac37145e6286248bfad5b
Author: Alex Herbert <[email protected]>
AuthorDate: Sat Aug 22 12:11:53 2026 +0100
RNG-197: Drop methods not implemented by java.util.Random
The method defaults from UniformRandomProvider are inherited using the
Random.nextLong() as the source of randomness.
Requires disabling japicmp for the simple module as the plugin does not
recognise the method exists as it does not use package dependencies to
find the interface default implementation. The change is accepted by
revapi.
---
.../commons/rng/simple/JDKRandomWrapper.java | 31 +-------
.../{profile.japicmp => profile.japicmp.disabled} | 5 ++
.../commons/rng/simple/JDKRandomWrapperTest.java | 83 ----------------------
src/changes/changes.xml | 5 ++
4 files changed, 13 insertions(+), 111 deletions(-)
diff --git
a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomWrapper.java
b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomWrapper.java
index 84f52b83..9d41fa26 100644
---
a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomWrapper.java
+++
b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomWrapper.java
@@ -23,6 +23,9 @@ import java.util.Random;
/**
* Wraps a {@link Random} instance to implement {@link UniformRandomProvider}.
All methods from
* the {@code Random} that match those in {@code UniformRandomProvider} are
used directly.
+ * Methods not specified by {@link Random} default to the implementation
provided by the
+ * {@link UniformRandomProvider} interface using {@link Random#nextLong()} as
the source
+ * of randomness.
*
* <p>This class can be used to wrap an instance of
* {@link java.security.SecureRandom SecureRandom}. The {@code SecureRandom}
class provides
@@ -57,16 +60,6 @@ public final class JDKRandomWrapper implements
UniformRandomProvider {
rng.nextBytes(bytes);
}
- /** {@inheritDoc} */
- @Override
- public void nextBytes(byte[] bytes,
- int start,
- int len) {
- final byte[] reduced = new byte[len];
- rng.nextBytes(reduced);
- System.arraycopy(reduced, 0, bytes, start, len);
- }
-
/** {@inheritDoc} */
@Override
public int nextInt() {
@@ -85,24 +78,6 @@ public final class JDKRandomWrapper implements
UniformRandomProvider {
return rng.nextLong();
}
- /** {@inheritDoc} */
- @Override
- public long nextLong(long n) {
- // Code copied from "o.a.c.rng.core.BaseProvider".
- if (n <= 0) {
- throw new IllegalArgumentException("Must be strictly positive: " +
n);
- }
-
- long bits;
- long val;
- do {
- bits = nextLong() >>> 1;
- val = bits % n;
- } while (bits - val + (n - 1) < 0);
-
- return val;
- }
-
/** {@inheritDoc} */
@Override
public boolean nextBoolean() {
diff --git a/commons-rng-simple/src/site/resources/profile.japicmp
b/commons-rng-simple/src/site/resources/profile.japicmp.disabled
similarity index 81%
rename from commons-rng-simple/src/site/resources/profile.japicmp
rename to commons-rng-simple/src/site/resources/profile.japicmp.disabled
index 6fe28ff3..27cbc8ce 100644
--- a/commons-rng-simple/src/site/resources/profile.japicmp
+++ b/commons-rng-simple/src/site/resources/profile.japicmp.disabled
@@ -15,3 +15,8 @@
# -----------------------------------------------------------------------------
#
# Empty file used to automatically trigger profile from commons parent pom
+#
+# TODO: Enable JApiCmp
+# This has been disabled for the 1.7 to 1.8 comparison.
+# JApiCmp does not recognise methods moved from JDKRandomWrapper to the default
+# implementation in the UniformRandomProvider interface.
diff --git
a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/JDKRandomWrapperTest.java
b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/JDKRandomWrapperTest.java
index 54b057be..97b2dc95 100644
---
a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/JDKRandomWrapperTest.java
+++
b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/JDKRandomWrapperTest.java
@@ -19,7 +19,6 @@ package org.apache.commons.rng.simple;
import java.util.Random;
import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.core.source64.LongProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -83,86 +82,4 @@ class JDKRandomWrapperTest {
store2[i]);
}
}
-
- /**
- * Test {@link UniformRandomProvider#nextLong(long)} matches that from the
core
- * BaseProvider implementation.
- */
- @Test
- void testNextLongInRange() {
- final long seed = RandomSource.createLong();
- // This will use the RNG core BaseProvider implementation.
- // Use a LongProvider to directly use the Random::nextLong method
- // which is different from IntProvider::nextLong.
- final UniformRandomProvider rng1 = new LongProvider() {
- private final Random random = new Random(seed);
-
- @Override
- public long next() {
- return random.nextLong();
- }
- };
- final UniformRandomProvider rng2 = new JDKRandomWrapper(new
Random(seed));
-
- // Test cases
- // 1 : Smallest range
- // 256 : Integer power of 2
- // 56757 : Integer range
- // 1L << 32 : Non-integer power of 2
- // (1L << 62) + 1 : Worst case for rejection rate for the algorithm.
- // Reject probability is approximately 0.5 thus the
test hits
- // all code paths.
- for (final long max : new long[] {1, 256, 56757, 1L << 32, (1L << 62)
+ 1}) {
- for (int i = 0; i < 10; i++) {
- Assertions.assertEquals(rng1.nextLong(max),
- rng2.nextLong(max));
- }
- }
- }
-
- @Test
- void testNextLongInRangeThrows() {
- final UniformRandomProvider rng1 = new JDKRandomWrapper(new
Random(5675767L));
- Assertions.assertThrows(IllegalArgumentException.class, () ->
rng1.nextLong(0));
- }
-
- /**
- * Test the bytes created by {@link
UniformRandomProvider#nextBytes(byte[], int, int)} matches
- * {@link Random#nextBytes(byte[])}.
- */
- @Test
- void testNextByteInRange() {
- final long seed = RandomSource.createLong();
- final Random rng1 = new Random(seed);
- final UniformRandomProvider rng2 = new JDKRandomWrapper(new
Random(seed));
-
- checkSameBytes(rng1, rng2, 1, 0, 1);
- checkSameBytes(rng1, rng2, 100, 0, 100);
- checkSameBytes(rng1, rng2, 100, 10, 90);
- checkSameBytes(rng1, rng2, 245, 67, 34);
- }
-
- /**
- * Ensure that the bytes produced in a sub-range of a byte array by
- * {@link UniformRandomProvider#nextBytes(byte[], int, int)} match the
bytes created
- * by the JDK {@link Random#nextBytes(byte[])}.
- *
- * @param rng1 JDK Random.
- * @param rng2 RNG.
- * @param size Size of byte array.
- * @param start Index at which to start inserting the generated bytes.
- * @param len Number of bytes to insert.
- */
- private static void checkSameBytes(Random rng1,
- UniformRandomProvider rng2,
- int size, int start, int length) {
- final byte[] store1 = new byte[length];
- final byte[] store2 = new byte[size];
- rng1.nextBytes(store1);
- rng2.nextBytes(store2, start, length);
- for (int i = 0; i < length; i++) {
- Assertions.assertEquals(store1[i],
- store2[i + start]);
- }
- }
}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b4aa4bb4..4b758d9d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,11 @@ If the output is not quite correct, check for invisible
trailing spaces!
<release version="1.8" date="TBD" description="
New features, updates and bug fixes (requires Java 8).
">
+ <action dev="aherbert" type="update" due-to="Alex Herbert"
issue="RNG-197">
+ "JDKRandomWrapper": Methods not directly implemented by
java.util.Random
+ use the default implementation in UniformRandomProvider with
Random.nextLong()
+ as the source of randomness.
+ </action>
<action dev="aherbert" type="update" due-to="Security scan, Alex
Herbert">
"ISAACRandom": Document unsuitability for use in a cryptographic or
security
purposes due to the use of a non-cryptographically strong source of
entropy for