-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathRandomPickDemo.java
53 lines (40 loc) · 1.17 KB
/
RandomPickDemo.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
package ds_001_intro;
import java.util.concurrent.ThreadLocalRandom;
public class RandomPickDemo {
public static void main(String[] args) {
// Original Array
int[] intArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Number of Random Subset Elements
int subsetNum = 5;
int[] randomPicks = getRandomNArray(intArray, subsetNum);
// Print Random Subset
printArray(randomPicks);
}
//
private static int[] getRandomNArray(int[] intArray, int N) {
int[] randomPicks = new int[N];
boolean[] isPicked = new boolean[intArray.length];
int numPicked = 0;
do {
int i = getRandom(0, intArray.length);
if(!isPicked[i]) {
randomPicks[numPicked] = intArray[i];
isPicked[i] = true;
numPicked++;
}
} while(numPicked < N);
return randomPicks;
}
// Generate Random number from the interval [a, b)
private static int getRandom(int i, int length) {
return ThreadLocalRandom.current().nextInt(i, length);
}
// Prints a 1D integer array
private static void printArray(int[] intArray) {
System.out.print("Array: ");
for(int i = 0; i < intArray.length; i++) {
System.out.printf("%2d", intArray[i]);
}
System.out.printf("\n");
}
}