SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RandomPermutation.java
1/*
2 * Class: RandomPermutation
3 * Description: Provides methods to randomly shuffle arrays or lists
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.rng;
26
27import java.util.Collections;
28import java.util.List;
29import java.util.ListIterator;
30import java.util.RandomAccess;
31
37public class RandomPermutation {
38 private static final int SHUFFLE_THRESHOLD = 5;
39
48 @Deprecated
49 public static void init(byte[] array, int n) {
50 for (byte k = 1; k <= n; k++)
51 array[k - 1] = k;
52 }
53
60 @Deprecated
61 public static void init(short[] array, int n) {
62 for (short k = 1; k <= n; k++)
63 array[k - 1] = k;
64 }
65
72 @Deprecated
73 public static void init(int[] array, int n) {
74 for (int k = 1; k <= n; k++)
75 array[k - 1] = k;
76 }
77
84 @Deprecated
85 public static void init(long[] array, int n) {
86 for (int k = 1; k <= n; k++)
87 array[k - 1] = k;
88 }
89
96 @Deprecated
97 public static void init(float[] array, int n) {
98 for (int k = 1; k <= n; k++)
99 array[k - 1] = k;
100 }
101
108 @Deprecated
109 public static void init(double[] array, int n) {
110 for (int k = 1; k <= n; k++)
111 array[k - 1] = k;
112 }
113
121 @SuppressWarnings("unchecked")
122 public static void shuffle(List<?> list, RandomStream stream) {
123 // The implementation is inspired from Sun's Collections.shuffle
124 final int size = list.size();
125 if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
126 for (int i = size; i > 1; i--)
127 Collections.swap(list, i - 1, stream.nextInt(0, i - 1));
128
129 } else {
130 final Object arr[] = list.toArray();
131
132 // Shuffle array<
133 shuffle(arr, stream);
134
135 // Dump array back into list
136 final ListIterator it = list.listIterator();
137 for (Object element : arr) {
138 it.next();
139 it.set(element);
140 }
141 }
142 }
143
151 public static void shuffle(Object[] array, RandomStream stream) {
152 final int size = array.length;
153 for (int i = size - 1; i > 0; i--) {
154 final int j = stream.nextInt(0, i);
155 final Object tmp = array[i];
156 array[i] = array[j];
157 array[j] = tmp;
158 }
159 }
160
168 public static void shuffle(byte[] array, RandomStream stream) {
169 final int size = array.length;
170 for (int i = size - 1; i > 0; i--) {
171 final int j = stream.nextInt(0, i);
172 final byte tmp = array[i];
173 array[i] = array[j];
174 array[j] = tmp;
175 }
176 }
177
184 public static void shuffle(short[] array, RandomStream stream) {
185 final int size = array.length;
186 for (int i = size - 1; i > 0; i--) {
187 final int j = stream.nextInt(0, i);
188 final short tmp = array[i];
189 array[i] = array[j];
190 array[j] = tmp;
191 }
192 }
193
200 public static void shuffle(int[] array, RandomStream stream) {
201 final int size = array.length;
202 for (int i = size - 1; i > 0; i--) {
203 final int j = stream.nextInt(0, i);
204 final int tmp = array[i];
205 array[i] = array[j];
206 array[j] = tmp;
207 }
208 }
209
216 public static void shuffle(long[] array, RandomStream stream) {
217 final int size = array.length;
218 for (int i = size - 1; i > 0; i--) {
219 final int j = stream.nextInt(0, i);
220 final long tmp = array[i];
221 array[i] = array[j];
222 array[j] = tmp;
223 }
224 }
225
232 public static void shuffle(char[] array, RandomStream stream) {
233 final int size = array.length;
234 for (int i = size - 1; i > 0; i--) {
235 final int j = stream.nextInt(0, i);
236 final char tmp = array[i];
237 array[i] = array[j];
238 array[j] = tmp;
239 }
240 }
241
248 public static void shuffle(boolean[] array, RandomStream stream) {
249 final int size = array.length;
250 for (int i = size - 1; i > 0; i--) {
251 final int j = stream.nextInt(0, i);
252 final boolean tmp = array[i];
253 array[i] = array[j];
254 array[j] = tmp;
255 }
256 }
257
264 public static void shuffle(float[] array, RandomStream stream) {
265 final int size = array.length;
266 for (int i = size - 1; i > 0; i--) {
267 final int j = stream.nextInt(0, i);
268 final float tmp = array[i];
269 array[i] = array[j];
270 array[j] = tmp;
271 }
272 }
273
281 public static void shuffle(double[] array, RandomStream stream) {
282 final int size = array.length;
283 for (int i = size - 1; i > 0; i--) {
284 final int j = stream.nextInt(0, i);
285 final double tmp = array[i];
286 array[i] = array[j];
287 array[j] = tmp;
288 }
289 }
290
303 @SuppressWarnings("unchecked")
304 public static void shuffle(List<?> list, int k, RandomStream stream) {
305 // @precondition 0 <= k <= n <= size.
306
307 // The implementation is inspired from Sun's Collections.shuffle
308 final int size = list.size();
309 if (k < 0 || k > size)
310 throw new IllegalArgumentException("k must be 0 <= k <= list.size()");
311 if (0 == k)
312 return;
313 if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
314 for (int i = 0; i < k; i++) {
315 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
316 int j = stream.nextInt(i, size - 1);
317 Collections.swap(list, i, j);
318 }
319
320 } else {
321 final Object arr[] = list.toArray();
322
323 // Shuffle array<
324 shuffle(arr, size, k, stream);
325
326 // Dump array back into list
327 final ListIterator it = list.listIterator();
328 for (Object element : arr) {
329 it.next();
330 it.set(element);
331 }
332 }
333 }
334
348 public static void shuffle(Object[] array, int n, int k, RandomStream stream) {
349 // @precondition 0 <= k <= n <= a.length.
350 // Replace by
351 // if (k < 0 || k > n) throw new IllegalArgumentException();
352 // or at least assert 0 <= k && k <= n;
353 if (k < 0 || k > n)
354 throw new IllegalArgumentException("k must be 0 <= k <= n");
355 for (int i = 0; i < k; i++) {
356 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
357 int j = stream.nextInt(i, n - 1);
358 Object temp = array[j];
359 array[j] = array[i];
360 array[i] = temp;
361 }
362 }
363
372 public static void shuffle(byte[] array, int n, int k, RandomStream stream) {
373 // @precondition 0 <= k <= n <= a.length.
374 if (k < 0 || k > n)
375 throw new IllegalArgumentException("k must be 0 <= k <= n");
376 for (int i = 0; i < k; i++) {
377 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
378 int j = stream.nextInt(i, n - 1);
379 byte temp = array[j];
380 array[j] = array[i];
381 array[i] = temp;
382 }
383 }
384
393 public static void shuffle(short[] array, int n, int k, RandomStream stream) {
394 // @precondition 0 <= k <= n <= a.length.
395 if (k < 0 || k > n)
396 throw new IllegalArgumentException("k must be 0 <= k <= n");
397 for (int i = 0; i < k; i++) {
398 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
399 int j = stream.nextInt(i, n - 1);
400 short temp = array[j];
401 array[j] = array[i];
402 array[i] = temp;
403 }
404 }
405
414 public static void shuffle(int[] array, int n, int k, RandomStream stream) {
415 // @precondition 0 <= k <= n <= a.length.
416 if (k < 0 || k > n)
417 throw new IllegalArgumentException("k must be 0 <= k <= n");
418 for (int i = 0; i < k; i++) {
419 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
420 int j = stream.nextInt(i, n - 1);
421 int temp = array[j];
422 array[j] = array[i];
423 array[i] = temp;
424 }
425 }
426
435 public static void shuffle(long[] array, int n, int k, RandomStream stream) {
436 // @precondition 0 <= k <= n <= a.length.
437 if (k < 0 || k > n)
438 throw new IllegalArgumentException("k must be 0 <= k <= n");
439 for (int i = 0; i < k; i++) {
440 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
441 int j = stream.nextInt(i, n - 1);
442 long temp = array[j];
443 array[j] = array[i];
444 array[i] = temp;
445 }
446 }
447
456 public static void shuffle(char[] array, int n, int k, RandomStream stream) {
457 // @precondition 0 <= k <= n <= a.length.
458 if (k < 0 || k > n)
459 throw new IllegalArgumentException("k must be 0 <= k <= n");
460 for (int i = 0; i < k; i++) {
461 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
462 int j = stream.nextInt(i, n - 1);
463 char temp = array[j];
464 array[j] = array[i];
465 array[i] = temp;
466 }
467 }
468
477 public static void shuffle(boolean[] array, int n, int k, RandomStream stream) {
478 // @precondition 0 <= k <= n <= a.length.
479 if (k < 0 || k > n)
480 throw new IllegalArgumentException("k must be 0 <= k <= n");
481 for (int i = 0; i < k; i++) {
482 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
483 int j = stream.nextInt(i, n - 1);
484 boolean temp = array[j];
485 array[j] = array[i];
486 array[i] = temp;
487 }
488 }
489
498 public static void shuffle(float[] array, int n, int k, RandomStream stream) {
499 // @precondition 0 <= k <= n <= a.length.
500 if (k < 0 || k > n)
501 throw new IllegalArgumentException("k must be 0 <= k <= n");
502 for (int i = 0; i < k; i++) {
503 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
504 int j = stream.nextInt(i, n - 1);
505 float temp = array[j];
506 array[j] = array[i];
507 array[i] = temp;
508 }
509 }
510
519 public static void shuffle(double[] array, int n, int k, RandomStream stream) {
520 // @precondition 0 <= k <= n <= a.length.
521 if (k < 0 || k > n)
522 throw new IllegalArgumentException("k must be 0 <= k <= n");
523 for (int i = 0; i < k; i++) {
524 // Get random j in {i,...,n-1} and interchange a[i] with a[j].
525 int j = stream.nextInt(i, n - 1);
526 double temp = array[j];
527 array[j] = array[i];
528 array[i] = temp;
529 }
530 }
531
532}
Provides methods to randomly shuffle arrays or lists using a random stream.
static void init(int[] array, int n)
Similar to init(byte[], int).
static void init(double[] array, int n)
Similar to init(byte[], int).
static void init(short[] array, int n)
Similar to init(byte[], int).
static void shuffle(long[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(byte[] array, RandomStream stream)
Randomly permutes array using stream.
static void shuffle(Object[] array, int n, int k, RandomStream stream)
Partially permutes array as follows using stream: draws the new.
static void shuffle(int[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(short[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(int[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void init(byte[] array, int n)
Initializes array with the first positive integers in natural order as array , for .
static void init(long[] array, int n)
Similar to init(byte[], int).
static void shuffle(boolean[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void shuffle(List<?> list, RandomStream stream)
Same as java.util.Collections.shuffle(List<?
static void shuffle(byte[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void shuffle(boolean[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(float[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void init(float[] array, int n)
Similar to init(byte[], int).
static void shuffle(Object[] array, RandomStream stream)
Randomly permutes array using stream.
static void shuffle(short[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void shuffle(char[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(double[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void shuffle(float[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(char[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
static void shuffle(double[] array, RandomStream stream)
Similar to shuffle(byte[], RandomStream).
static void shuffle(long[] array, int n, int k, RandomStream stream)
Similar to shuffle(Object[], n, k, RandomStream).
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
int nextInt(int i, int j)
Returns a (pseudo)random number from the discrete uniform distribution over the integers ,...