-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRandomPermutation.java
66 lines (58 loc) · 1.4 KB
/
RandomPermutation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package Combinatorics;
import java.util.*;
//Random permutations, arrangements and shuffling
public class RandomPermutation {
public static void shuffle(int[] a) {
Random rnd = new Random();
for (int i = a.length - 1; i >= 1; i--) {
int j = rnd.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
public static int[] getRandomPermutation(int n) {
Random rnd = new Random();
int[] res = new int[n];
for (int i = 0; i < n; i++) {
int j = rnd.nextInt(i + 1);
res[i] = res[j];
res[j] = i;
}
return res;
}
public static int[] getRandomArrangement(int n, int m) {
Random rnd = new Random();
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = i;
for (int i = 0; i < m; i++) {
int j = i + rnd.nextInt(n - i);
int t = res[i];
res[i] = res[j];
res[j] = t;
}
return Arrays.copyOf(res, m);
}
public static int[] getRandomCombination(int n, int m) {
int[] res = getRandomArrangement(n, m);
Arrays.sort(res);
return res;
}
// for small m
public static int[] getRandomArrangement2(int n, int m) {
Random rnd = new Random();
Set<Integer> set = new HashSet<>();
int[] res = new int[m];
while (set.size() < m) {
int x = rnd.nextInt(n);
if (set.add(x))
res[set.size() - 1] = x;
}
return res;
}
// Usage example
public static void main(String[] args) {
System.out.println(Arrays.toString(getRandomPermutation(5)));
}
}