2121 */
2222package org .cicirello .permutations ;
2323
24- import java .util .Random ;
25- import java .util .SplittableRandom ;
24+ import java .util .random .RandomGenerator ;
2625import java .util .concurrent .ThreadLocalRandom ;
2726import java .util .Arrays ;
2827import java .io .Serializable ;
@@ -51,7 +50,7 @@ public final class Permutation implements Serializable, Iterable<Permutation>, C
5150
5251 /**
5352 * Initializes a random permutation of n integers. Uses
54- * java.util.concurrent. ThreadLocalRandom as the source of efficient random number generation.
53+ * {@link ThreadLocalRandom} as the source of efficient random number generation.
5554 * @param n the length of the permutation
5655 */
5756 public Permutation (int n ) {
@@ -64,17 +63,7 @@ public Permutation(int n) {
6463 * @param n the length of the permutation
6564 * @param r A source of randomness.
6665 */
67- public Permutation (int n , SplittableRandom r ) {
68- permutation = new int [n ];
69- scramble (r );
70- }
71-
72- /**
73- * Initializes a random permutation of n integers.
74- * @param n the length of the permutation
75- * @param r A source of randomness.
76- */
77- public Permutation (int n , Random r ) {
66+ public Permutation (int n , RandomGenerator r ) {
7867 permutation = new int [n ];
7968 scramble (r );
8069 }
@@ -337,7 +326,7 @@ public void invert() {
337326
338327 /**
339328 * Randomly shuffles the permutation. Uses
340- * java.util.concurrent. ThreadLocalRandom as
329+ * {@link ThreadLocalRandom} as
341330 * the source of efficient random number generation.
342331 */
343332 public void scramble () {
@@ -348,30 +337,7 @@ public void scramble() {
348337 * Randomly shuffles the permutation.
349338 * @param r a source of randomness.
350339 */
351- public void scramble (Random r ) {
352- if (permutation .length > 0 ) {
353- // Since we're scrambling entire permutation, just generate a new
354- // permutation of integers in [0, n).
355- // Avoid swapping using trick described in Knuth, Vol 2, page 145,
356- // last complete paragraph.
357- permutation [0 ] = 0 ;
358- for (int i = 1 ; i < permutation .length ; i ++) {
359- int j = RandomIndexer .nextInt (i +1 , r );
360- if (j == i ) {
361- permutation [i ] = i ;
362- } else {
363- permutation [i ] = permutation [j ];
364- permutation [j ] = i ;
365- }
366- }
367- }
368- }
369-
370- /**
371- * Randomly shuffles the permutation.
372- * @param r a source of randomness.
373- */
374- public void scramble (SplittableRandom r ) {
340+ public void scramble (RandomGenerator r ) {
375341 if (permutation .length > 0 ) {
376342 // Since we're scrambling entire permutation, just generate a new
377343 // permutation of integers in [0, n).
@@ -392,7 +358,7 @@ public void scramble(SplittableRandom r) {
392358
393359 /**
394360 * Randomly shuffles the permutation. Uses
395- * java.util.concurrent. ThreadLocalRandom as
361+ * {@link ThreadLocalRandom} as
396362 * the source of efficient random number generation.
397363 *
398364 * @param guaranteeDifferent if true and if permutation length is at least 2, then method
@@ -409,32 +375,7 @@ public void scramble(boolean guaranteeDifferent) {
409375 * @param guaranteeDifferent if true and if permutation length is at least 2, then method
410376 * guarantees that the result is a different permutation than it was originally.
411377 */
412- public void scramble (Random r , boolean guaranteeDifferent ) {
413- if (guaranteeDifferent ) {
414- boolean changed = false ;
415- for (int i = permutation .length - 1 ; i > 1 ; i --) {
416- int j = RandomIndexer .nextInt (i +1 , r );
417- if (i != j ) {
418- swap (i ,j );
419- changed = true ;
420- }
421- }
422- if (permutation .length > 1 && (!changed || r .nextBoolean ())) {
423- swap (0 ,1 );
424- }
425- } else {
426- scramble (r );
427- }
428- }
429-
430- /**
431- * Randomly shuffles the permutation.
432- *
433- * @param r a source of randomness.
434- * @param guaranteeDifferent if true and if permutation length is at least 2, then method
435- * guarantees that the result is a different permutation than it was originally.
436- */
437- public void scramble (SplittableRandom r , boolean guaranteeDifferent ) {
378+ public void scramble (RandomGenerator r , boolean guaranteeDifferent ) {
438379 if (guaranteeDifferent ) {
439380 boolean changed = false ;
440381 for (int i = permutation .length - 1 ; i > 1 ; i --) {
@@ -454,7 +395,7 @@ public void scramble(SplittableRandom r, boolean guaranteeDifferent) {
454395
455396 /**
456397 * Randomly shuffles a segment. Uses
457- * java.util.concurrent. ThreadLocalRandom as
398+ * {@link ThreadLocalRandom} as
458399 * the source of efficient random number generation.
459400 * @param i endpoint of the segment
460401 * (precondition: 0 ≤ i < length())
@@ -477,7 +418,7 @@ public void scramble(int i, int j) {
477418 * @throws ArrayIndexOutOfBoundsException if either i or j are negative,
478419 * or if either i or j are greater than or equal to length()
479420 */
480- public void scramble (int i , int j , Random r ) {
421+ public void scramble (int i , int j , RandomGenerator r ) {
481422 if (i ==j ) { return ; }
482423 if (i > j ) {
483424 int temp = i ;
@@ -497,63 +438,6 @@ public void scramble(int i, int j, Random r) {
497438 }
498439 }
499440
500- /**
501- * Randomly shuffles a segment.
502- * @param i endpoint of the segment
503- * (precondition: 0 ≤ i < length())
504- * @param j endpoint of the segment
505- * (precondition: 0 ≤ j < length())
506- * @param r source of randomness
507- * @throws ArrayIndexOutOfBoundsException if either i or j are negative,
508- * or if either i or j are greater than or equal to length()
509- */
510- public void scramble (int i , int j , SplittableRandom r ) {
511- if (i ==j ) { return ; }
512- if (i > j ) {
513- int temp = i ;
514- i = j ;
515- j = temp ;
516- }
517- boolean changed = false ;
518- for (int k = j ; k > i + 1 ; k --) {
519- int l = i + RandomIndexer .nextInt (k -i +1 , r );
520- if (l != k ) {
521- swap (l ,k );
522- changed = true ;
523- }
524- }
525- if (!changed || r .nextBoolean ()) {
526- swap (i ,i +1 );
527- }
528- }
529-
530- /**
531- * Randomly shuffles a non-contiguous set of permutation elements. As long as there
532- * are at least 2 different indexes passed to this method, it is guaranteed to
533- * change the Permutation.
534- * @param indexes An array of indexes into the permutation. This method assumes
535- * that the indexes are valid indexes into the permutation. That is, it assumes
536- * that 0 ≤ indexes[i] < this.length().
537- * @param r source of randomness
538- * @throws ArrayIndexOutOfBoundsException if any of the indexes[i] are negative or
539- * greater than or equal to this.length().
540- */
541- public void scramble (int [] indexes , SplittableRandom r ) {
542- if (indexes .length > 1 ) {
543- boolean changed = false ;
544- for (int j = indexes .length -1 ; j > 1 ; j --) {
545- int i = RandomIndexer .nextInt (j +1 , r );
546- if (i != j ) {
547- swap (indexes [i ],indexes [j ]);
548- changed = true ;
549- }
550- }
551- if (!changed || r .nextBoolean ()) {
552- swap (indexes [0 ],indexes [1 ]);
553- }
554- }
555- }
556-
557441 /**
558442 * Randomly shuffles a non-contiguous set of permutation elements. As long as there
559443 * 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) {
565449 * @throws ArrayIndexOutOfBoundsException if any of the indexes[i] are negative or
566450 * greater than or equal to this.length().
567451 */
568- public void scramble (int [] indexes , Random r ) {
452+ public void scramble (int [] indexes , RandomGenerator r ) {
569453 if (indexes .length > 1 ) {
570454 boolean changed = false ;
571455 for (int j = indexes .length -1 ; j > 1 ; j --) {
0 commit comments