-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP23.java
More file actions
42 lines (36 loc) · 1.04 KB
/
P23.java
File metadata and controls
42 lines (36 loc) · 1.04 KB
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
package lists;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Select random from list.
*/
public final class P23 {
private P23() {
}
/**
* Random select count of items.
*
* @param list list of item
* @param count number of item to pick up
* @param <T> type of item
* @return random selected item.
*/
public static <T> List<T> randomSelect(final List<T> list,
final int count) {
List<T> resArr = new ArrayList<>();
List<T> actLst = list;
for (int i = 1; i <= count; i++) {
Object[] objects = P20.removeAt(actLst,
getRandomNumberInRange(actLst.size()));
actLst = (List<T>) objects[0];
resArr.add((T) objects[1]);
}
return resArr;
}
private static int getRandomNumberInRange(final int max) {
Random r = new SecureRandom();
return r.nextInt((max - 1) + 1) + 1;
}
}