From d4de78b211c15815b12baa011a0574d2eb9ee830 Mon Sep 17 00:00:00 2001 From: "Vincent A. Cicirello" Date: Thu, 2 Jun 2022 15:13:13 -0400 Subject: [PATCH 1/4] reduced redundant code with RandomGenerator interface --- .../cicirello/permutations/Permutation.java | 136 ++---------------- 1 file changed, 10 insertions(+), 126 deletions(-) diff --git a/src/main/java/org/cicirello/permutations/Permutation.java b/src/main/java/org/cicirello/permutations/Permutation.java index d87e8c64..0fa53dbb 100644 --- a/src/main/java/org/cicirello/permutations/Permutation.java +++ b/src/main/java/org/cicirello/permutations/Permutation.java @@ -21,8 +21,7 @@ */ package org.cicirello.permutations; -import java.util.Random; -import java.util.SplittableRandom; +import java.util.random.RandomGenerator; import java.util.concurrent.ThreadLocalRandom; import java.util.Arrays; import java.io.Serializable; @@ -51,7 +50,7 @@ public final class Permutation implements Serializable, Iterable, C /** * Initializes a random permutation of n integers. Uses - * java.util.concurrent.ThreadLocalRandom as the source of efficient random number generation. + * {@link ThreadLocalRandom} as the source of efficient random number generation. * @param n the length of the permutation */ public Permutation(int n) { @@ -64,17 +63,7 @@ public Permutation(int n) { * @param n the length of the permutation * @param r A source of randomness. */ - public Permutation(int n, SplittableRandom r) { - permutation = new int[n]; - scramble(r); - } - - /** - * Initializes a random permutation of n integers. - * @param n the length of the permutation - * @param r A source of randomness. - */ - public Permutation(int n, Random r) { + public Permutation(int n, RandomGenerator r) { permutation = new int[n]; scramble(r); } @@ -337,7 +326,7 @@ public void invert() { /** * Randomly shuffles the permutation. Uses - * java.util.concurrent.ThreadLocalRandom as + * {@link ThreadLocalRandom} as * the source of efficient random number generation. */ public void scramble() { @@ -348,30 +337,7 @@ public void scramble() { * Randomly shuffles the permutation. * @param r a source of randomness. */ - public void scramble(Random r) { - if (permutation.length > 0) { - // Since we're scrambling entire permutation, just generate a new - // permutation of integers in [0, n). - // Avoid swapping using trick described in Knuth, Vol 2, page 145, - // last complete paragraph. - permutation[0] = 0; - for (int i = 1; i < permutation.length; i++) { - int j = RandomIndexer.nextInt(i+1, r); - if (j == i) { - permutation[i] = i; - } else { - permutation[i] = permutation[j]; - permutation[j] = i; - } - } - } - } - - /** - * Randomly shuffles the permutation. - * @param r a source of randomness. - */ - public void scramble(SplittableRandom r) { + public void scramble(RandomGenerator r) { if (permutation.length > 0) { // Since we're scrambling entire permutation, just generate a new // permutation of integers in [0, n). @@ -392,7 +358,7 @@ public void scramble(SplittableRandom r) { /** * Randomly shuffles the permutation. Uses - * java.util.concurrent.ThreadLocalRandom as + * {@link ThreadLocalRandom} as * the source of efficient random number generation. * * @param guaranteeDifferent if true and if permutation length is at least 2, then method @@ -409,32 +375,7 @@ public void scramble(boolean guaranteeDifferent) { * @param guaranteeDifferent if true and if permutation length is at least 2, then method * guarantees that the result is a different permutation than it was originally. */ - public void scramble(Random r, boolean guaranteeDifferent) { - if (guaranteeDifferent) { - boolean changed = false; - for (int i = permutation.length - 1; i > 1; i--) { - int j = RandomIndexer.nextInt(i+1, r); - if (i != j) { - swap(i,j); - changed = true; - } - } - if (permutation.length > 1 && (!changed || r.nextBoolean())) { - swap(0,1); - } - } else { - scramble(r); - } - } - - /** - * Randomly shuffles the permutation. - * - * @param r a source of randomness. - * @param guaranteeDifferent if true and if permutation length is at least 2, then method - * guarantees that the result is a different permutation than it was originally. - */ - public void scramble(SplittableRandom r, boolean guaranteeDifferent) { + public void scramble(RandomGenerator r, boolean guaranteeDifferent) { if (guaranteeDifferent) { boolean changed = false; for (int i = permutation.length - 1; i > 1; i--) { @@ -454,7 +395,7 @@ public void scramble(SplittableRandom r, boolean guaranteeDifferent) { /** * Randomly shuffles a segment. Uses - * java.util.concurrent.ThreadLocalRandom as + * {@link ThreadLocalRandom} as * the source of efficient random number generation. * @param i endpoint of the segment * (precondition: 0 ≤ i < length()) @@ -477,7 +418,7 @@ public void scramble(int i, int j) { * @throws ArrayIndexOutOfBoundsException if either i or j are negative, * or if either i or j are greater than or equal to length() */ - public void scramble(int i, int j, Random r) { + public void scramble(int i, int j, RandomGenerator r) { if (i==j) { return; } if (i > j) { int temp = i; @@ -497,63 +438,6 @@ public void scramble(int i, int j, Random r) { } } - /** - * Randomly shuffles a segment. - * @param i endpoint of the segment - * (precondition: 0 ≤ i < length()) - * @param j endpoint of the segment - * (precondition: 0 ≤ j < length()) - * @param r source of randomness - * @throws ArrayIndexOutOfBoundsException if either i or j are negative, - * or if either i or j are greater than or equal to length() - */ - public void scramble(int i, int j, SplittableRandom r) { - if (i==j) { return; } - if (i > j) { - int temp = i; - i = j; - j = temp; - } - boolean changed = false; - for (int k = j; k > i + 1; k--) { - int l = i + RandomIndexer.nextInt(k-i+1, r); - if (l != k) { - swap(l,k); - changed = true; - } - } - if (!changed || r.nextBoolean()) { - swap(i,i+1); - } - } - - /** - * Randomly shuffles a non-contiguous set of permutation elements. As long as there - * are at least 2 different indexes passed to this method, it is guaranteed to - * change the Permutation. - * @param indexes An array of indexes into the permutation. This method assumes - * that the indexes are valid indexes into the permutation. That is, it assumes - * that 0 ≤ indexes[i] < this.length(). - * @param r source of randomness - * @throws ArrayIndexOutOfBoundsException if any of the indexes[i] are negative or - * greater than or equal to this.length(). - */ - public void scramble(int[] indexes, SplittableRandom r) { - if (indexes.length > 1) { - boolean changed = false; - for (int j = indexes.length-1; j > 1; j--) { - int i = RandomIndexer.nextInt(j+1, r); - if (i != j) { - swap(indexes[i],indexes[j]); - changed = true; - } - } - if (!changed || r.nextBoolean()) { - swap(indexes[0],indexes[1]); - } - } - } - /** * Randomly shuffles a non-contiguous set of permutation elements. As long as there * are at least 2 different indexes passed to this method, it is guaranteed to @@ -565,7 +449,7 @@ public void scramble(int[] indexes, SplittableRandom r) { * @throws ArrayIndexOutOfBoundsException if any of the indexes[i] are negative or * greater than or equal to this.length(). */ - public void scramble(int[] indexes, Random r) { + public void scramble(int[] indexes, RandomGenerator r) { if (indexes.length > 1) { boolean changed = false; for (int j = indexes.length-1; j > 1; j--) { From 90d8eda961a6f50608ee5714c4d23e875efc6b3e Mon Sep 17 00:00:00 2001 From: "Vincent A. Cicirello" Date: Thu, 2 Jun 2022 15:14:50 -0400 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8adc8d0..6c0329db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ details below. ### Changed * Minimum supported Java version is now Java 17 (breaking change). +* Utilized Java 17 RandomGenerator interface to eliminate redundant code. ### Deprecated From 66212a4022e2b66acbc7a3b3e55141ada4f2d09e Mon Sep 17 00:00:00 2001 From: "Vincent A. Cicirello" Date: Thu, 2 Jun 2022 15:16:21 -0400 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c0329db..bbc3e6e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ details below. ### Changed * Minimum supported Java version is now Java 17 (breaking change). -* Utilized Java 17 RandomGenerator interface to eliminate redundant code. +* Utilized Java 17 RandomGenerator interface to eliminate redundant code and to support all Java 17 random number generators. ### Deprecated From 25a7b1184fe18125451d13129eedf71e244c119b Mon Sep 17 00:00:00 2001 From: "Vincent A. Cicirello" Date: Thu, 2 Jun 2022 15:18:23 -0400 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbc3e6e0..5eaf86d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 details below. ### Added +* Support for all Java 17 random number generators in generating random Permutation objects. ### Changed * Minimum supported Java version is now Java 17 (breaking change). -* Utilized Java 17 RandomGenerator interface to eliminate redundant code and to support all Java 17 random number generators. +* Utilized Java 17 RandomGenerator interface to eliminate redundant code. ### Deprecated