Skip to content

Commit

Permalink
nextWindowedIntTriple returning IndexTriple
Browse files Browse the repository at this point in the history
  • Loading branch information
cicirello committed May 9, 2024
1 parent 6f2722b commit 71208f1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Versions of nextIntPair methods that utilize the new IndexPair class added to both RandomIndexer and EnhancedRandomGenerator classes.
* Versions of nextIntTriple methods that utilize the new IndexTriple class added to both RandomIndexer and EnhancedRandomGenerator classes.
* Versions of nextWindowedIntPair methods that utilize the new IndexPair class added to both RandomIndexer and EnhancedRandomGenerator classes.
* Versions of nextWindowedIntTriple methods that utilize the new IndexTriple class added to both RandomIndexer and EnhancedRandomGenerator classes.
* Streams of random pairs of distinct integers as streams of IndexPair objects.
* Streams of random triples of distinct integers as streams of IndexTriple objects.
* Streams of random triples of distinct sorted integers as streams of IndexTriple objects.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/cicirello/math/rand/EnhancedRandomGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,24 @@ public final int[] nextWindowedIntTriple(int n, int window, int[] result) {
return RandomIndexer.nextWindowedIntTriple(n, window, result, generator);
}

/**
* Generates a random sample of 3 integers, i, j, k without replacement, from the set of integers
* in the interval [0, n), such that |i-j| ≤ window, and |i-k| ≤ window, and |k-j| ≤
* window. All triples that satisfy the window constraint are equally likely. <b>Enhanced
* Functionality.</b>
*
* <p>The runtime is O(1).
*
* @param n The number of integers to choose from.
* @param window The maximum difference between the integers of the triple.
* @return A triple of randomly chosen integers, i, j, k from the interval [0, n), such that |i-j|
* &le; window, and |i-k| &le; window, and |k-j| &le; window.
* @throws IllegalArgumentException if window &lt; 2 or n &lt; 3.
*/
public final IndexTriple nextWindowedIntTriple(int n, int window) {
return RandomIndexer.nextWindowedIntTriple(n, window, generator);
}

/**
* Generates a random sample of 3 integers, i, j, k without replacement, from the set of integers
* in the interval [0, n), such that |i-j| &le; window, and |i-k| &le; window, and |k-j| &le;
Expand All @@ -620,6 +638,24 @@ public final int[] nextSortedWindowedIntTriple(int n, int window, int[] result)
return RandomIndexer.nextSortedWindowedIntTriple(n, window, result, generator);
}

/**
* Generates a random sample of 3 integers, i, j, k without replacement, from the set of integers
* in the interval [0, n), such that |i-j| &le; window, and |i-k| &le; window, and |k-j| &le;
* window. All triples that satisfy the window constraint are equally likely. The result is sorted
* in increasing order. <b>Enhanced Functionality.</b>
*
* <p>The runtime is O(1).
*
* @param n The number of integers to choose from.
* @param window The maximum difference between the integers of the triple.
* @return A triple of randomly chosen integers, i, j, k from the interval [0, n), such that |i-j|
* &le; window, and |i-k| &le; window, and |k-j| &le; window.
* @throws IllegalArgumentException if window &lt; 2 or n &lt; 3.
*/
public final IndexTriple nextSortedWindowedIntTriple(int n, int window) {
return RandomIndexer.nextSortedWindowedIntTriple(n, window, generator);
}

/**
* Returns an effectively unlimited stream of pseudorandom pairs of int values, without
* replacement, from the interval [0, n). <b>Enhanced Functionality.</b>
Expand Down
72 changes: 57 additions & 15 deletions src/test/java/org/cicirello/math/rand/ERGPairsTriplesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public void testNextIntPair() {
public void testNextIntPair_IndexPair() {
EnhancedRandomGenerator erg = new EnhancedRandomGenerator();
IndexPair result = erg.nextIntPair(6);
assertTrue(result.i() < 6);
assertTrue(result.j() < 6);
assertNotEquals(result.i(), result.j());
validateCombo(6, result);
}

@Test
Expand Down Expand Up @@ -79,12 +77,7 @@ public void testNextIntTripleSorted() {
public void testNextIntTriple_IndexTriple() {
EnhancedRandomGenerator erg = new EnhancedRandomGenerator();
IndexTriple result = erg.nextIntTriple(8);
assertTrue(result.i() < 8);
assertTrue(result.j() < 8);
assertTrue(result.k() < 8);
assertNotEquals(result.i(), result.j());
assertNotEquals(result.i(), result.k());
assertNotEquals(result.j(), result.k());
validateCombo(8, result);
}

@Test
Expand All @@ -111,12 +104,7 @@ public void testNextWindowedIntPair() {
public void testNextWindowedIntPair_IndexPair() {
EnhancedRandomGenerator erg = new EnhancedRandomGenerator();
IndexPair result = erg.nextWindowedIntPair(100, 5);
assertNotEquals(result.i(), result.j());
assertTrue(result.i() < 100);
assertTrue(result.j() < 100);
assertTrue(result.i() >= 0);
assertTrue(result.j() >= 0);
assertTrue(Math.abs(result.j() - result.i()) <= 5);
validateWindowed(100, 5, result);
}

@Test
Expand All @@ -129,6 +117,15 @@ public void testNextWindowedIntTriple() {
validateWindowed(8, 6, 3, result);
}

@Test
public void testNextWindowedIntTriple_IndexTriple() {
EnhancedRandomGenerator erg = new EnhancedRandomGenerator();
IndexTriple result = erg.nextWindowedIntTriple(100, 6);
validateWindowed(100, 6, result);
IndexTriple result2 = erg.nextWindowedIntTriple(8, 6);
validateWindowed(8, 6, result2);
}

@Test
public void testNextWindowedIntTripleSorted() {
EnhancedRandomGenerator erg = new EnhancedRandomGenerator();
Expand All @@ -143,6 +140,19 @@ public void testNextWindowedIntTripleSorted() {
assertTrue(result2[1] < result2[2]);
}

@Test
public void testNextWindowedIntTripleSorted_IndexTriple() {
EnhancedRandomGenerator erg = new EnhancedRandomGenerator();
IndexTriple result = erg.nextSortedWindowedIntTriple(100, 6);
validateWindowed(100, 6, result);
assertTrue(result.i() < result.j());
assertTrue(result.j() < result.k());
IndexTriple result2 = erg.nextSortedWindowedIntTriple(100, 6);
validateWindowed(100, 6, result2);
assertTrue(result2.i() < result2.j());
assertTrue(result2.j() < result2.k());
}

private void validateCombo(int n, int k, int[] result) {
assertEquals(k, result.length);
for (int i = 0; i < result.length; i++) {
Expand All @@ -153,6 +163,26 @@ private void validateCombo(int n, int k, int[] result) {
}
}

private void validateCombo(int n, IndexTriple result) {
assertTrue(result.i() < n);
assertTrue(result.j() < n);
assertTrue(result.k() < n);
assertTrue(result.i() >= 0);
assertTrue(result.j() >= 0);
assertTrue(result.k() >= 0);
assertNotEquals(result.i(), result.j());
assertNotEquals(result.i(), result.k());
assertNotEquals(result.k(), result.j());
}

private void validateCombo(int n, IndexPair result) {
assertTrue(result.i() < n);
assertTrue(result.j() < n);
assertTrue(result.i() >= 0);
assertTrue(result.j() >= 0);
assertNotEquals(result.i(), result.j());
}

private void validateWindowed(int n, int window, int k, int[] result) {
validateCombo(n, k, result);
for (int i = 0; i < result.length; i++) {
Expand All @@ -161,4 +191,16 @@ private void validateWindowed(int n, int window, int k, int[] result) {
}
}
}

private void validateWindowed(int n, int window, IndexTriple result) {
validateCombo(n, result);
assertTrue(Math.abs(result.i() - result.j()) <= window);
assertTrue(Math.abs(result.i() - result.k()) <= window);
assertTrue(Math.abs(result.j() - result.k()) <= window);
}

private void validateWindowed(int n, int window, IndexPair result) {
validateCombo(n, result);
assertTrue(Math.abs(result.i() - result.j()) <= window);
}
}

0 comments on commit 71208f1

Please sign in to comment.