|
1 | 1 | package javaToolkit.lib.utils;
|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
| 4 | +import java.util.HashSet; |
4 | 5 | import java.util.List;
|
5 | 6 | import java.util.Random;
|
| 7 | +import java.util.Set; |
6 | 8 |
|
7 | 9 | public class RandomUtil {
|
8 | 10 |
|
9 |
| - public Object getARandomElement(List<?> givenList) { |
10 |
| - Random rand = new Random(); |
11 |
| - Object randomElement = givenList.get(rand.nextInt(givenList.size())); |
12 |
| - return randomElement; |
13 |
| - } |
| 11 | + public Object getARandomElement(List<?> givenList) { |
| 12 | + Random rand = new Random(); |
| 13 | + Object randomElement = givenList.get(rand.nextInt(givenList.size())); |
| 14 | + return randomElement; |
| 15 | + } |
14 | 16 |
|
| 17 | + public List<Object> randomSelectWithRepeat(List<Object> givenList, int numberOfElements) { |
| 18 | + Random rand = new Random(); |
| 19 | + List<Object> selectedEles = new ArrayList<>(); |
15 | 20 |
|
16 |
| - public List<Object> randomSelectWithRepeat(List<Object> givenList, int numberOfElements) { |
17 |
| - Random rand = new Random(); |
18 |
| - List<Object> selectedEles = new ArrayList<>(); |
| 21 | + for (int i = 0; i < numberOfElements; i++) { |
| 22 | + int randomIndex = rand.nextInt(givenList.size()); |
| 23 | + Object randomElement = givenList.get(randomIndex); |
| 24 | + selectedEles.add(randomElement); |
| 25 | + } |
19 | 26 |
|
20 |
| - for (int i = 0; i < numberOfElements; i++) { |
21 |
| - int randomIndex = rand.nextInt(givenList.size()); |
22 |
| - Object randomElement = givenList.get(randomIndex); |
23 |
| - selectedEles.add(randomElement); |
24 |
| - } |
| 27 | + return selectedEles; |
| 28 | + } |
25 | 29 |
|
26 |
| - return selectedEles; |
27 |
| - } |
| 30 | + public List<?> randomSelectWithoutRepeat(List<?> givenList, int numberOfElements) { |
| 31 | + Random rand = new Random(); |
| 32 | + List<Object> selectedEles = new ArrayList<>(); |
28 | 33 |
|
29 |
| - public List<?> randomSelectWithoutRepeat(List<?> givenList, int numberOfElements) { |
30 |
| - Random rand = new Random(); |
31 |
| - List<Object> selectedEles = new ArrayList<>(); |
| 34 | + for (int i = 0; i < numberOfElements; i++) { |
| 35 | + int randomIndex = rand.nextInt(givenList.size()); |
| 36 | + Object randomElement = givenList.get(randomIndex); |
| 37 | + selectedEles.add(randomElement); |
| 38 | + givenList.remove(randomIndex); |
| 39 | + } |
32 | 40 |
|
33 |
| - for (int i = 0; i < numberOfElements; i++) { |
34 |
| - int randomIndex = rand.nextInt(givenList.size()); |
35 |
| - Object randomElement = givenList.get(randomIndex); |
36 |
| - selectedEles.add(randomElement); |
37 |
| - givenList.remove(randomIndex); |
38 |
| - } |
| 41 | + return selectedEles; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * include max and min |
| 46 | + * |
| 47 | + * @param min |
| 48 | + * @param max |
| 49 | + * @param k |
| 50 | + * @return |
| 51 | + */ |
| 52 | + public static Set<Integer> randomGenerateKDistinctNumbers(int min, int max, int k) { |
| 53 | + Random rand = new Random(); |
| 54 | + Set<Integer> selectedEles = new HashSet<>(); |
| 55 | + |
| 56 | + if (max - min < k) { |
| 57 | + System.out.printf("Cannot generate %s between %s and %s\n", k, min, max); |
| 58 | + System.exit(0); |
| 59 | + } |
| 60 | + |
| 61 | + while (selectedEles.size() < k) { |
| 62 | + int randomNum = rand.nextInt((max - min) + 1) + min; |
| 63 | + selectedEles.add(randomNum); |
| 64 | + } |
| 65 | + |
| 66 | + return selectedEles; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * include max and min, not contain |
| 71 | + * |
| 72 | + * @param min |
| 73 | + * @param max |
| 74 | + * @param k |
| 75 | + * @return |
| 76 | + * @throws Exception |
| 77 | + */ |
| 78 | + public static Set<Integer> randomKDistinctNumsWithoutSpecificNum(int min, int max, int k, int withoutNum) throws Exception { |
| 79 | + Random rand = new Random(); |
| 80 | + Set<Integer> selectedEles = new HashSet<>(); |
| 81 | + |
| 82 | + if (max - min < k) { |
| 83 | + System.out.printf("Cannot generate %s between %s and %s\n", k, min, max); |
| 84 | + throw new Exception("Cannot generate " + k + " between " + min + " and " + max + "\n"); |
| 85 | + } |
| 86 | + |
| 87 | + while (selectedEles.size() < k) { |
| 88 | + int randomNum = rand.nextInt((max - min) + 1) + min; |
| 89 | + if (randomNum != withoutNum) { |
| 90 | + selectedEles.add(randomNum); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return selectedEles; |
| 95 | + } |
39 | 96 |
|
40 |
| - return selectedEles; |
41 |
| - } |
42 | 97 | }
|
0 commit comments