Skip to content

Commit

Permalink
perf(utils): remove redundant validations in RandomUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio24 committed Jun 21, 2022
1 parent 82f95a3 commit cc5fa25
Showing 1 changed file with 9 additions and 7 deletions.
Expand Up @@ -8,14 +8,16 @@

public class RandomUtils extends org.apache.commons.lang3.RandomUtils {

public static final Random RANDOM = new Random();

/*================================================================================================================*/
// #region PickFromCollection

public static <T> T pickOneFromCollection(Collection<T> collection) {
Validate.notEmpty(collection, "The collection must be not empty");

int size = collection.size();
int target = nextInt(0, size);
int target = RANDOM.nextInt(size);
Iterator<T> iterator = collection.iterator();
for (int i = 0; i < target; i++) {
iterator.next();
Expand All @@ -40,13 +42,13 @@ public static <T> List<T> pickFromList(List<T> list, int N) {
List<T> newList = new ArrayList<T>();

if (N == 1) {
newList.add(list.get(nextInt(0, size)));
newList.add(list.get(RANDOM.nextInt(size)));
} else {
List<Integer> targets = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
int target;
while (true) {
target = nextInt(0, size);
target = RANDOM.nextInt(size);
if (!targets.contains(target)) {
targets.add(target);
break;
Expand All @@ -61,7 +63,7 @@ public static <T> List<T> pickFromList(List<T> list, int N) {

public static <T> T pickOneFromList(List<T> list) {
Validate.notEmpty(list, "The list must be not empty");
return list.get(nextInt(0, list.size()));
return list.get(RANDOM.nextInt(list.size()));
}

// #endregion PickFromCollection
Expand All @@ -71,7 +73,7 @@ public static <T> T pickOneFromList(List<T> list) {

public static boolean percentageChance(int chance) {
Validate.isInCloseInterval(0, 100, chance);
return nextInt(0, 100) < chance;
return RANDOM.nextInt(100) < chance;
}

public static boolean percentageChance(int chance, Random random) {
Expand All @@ -81,7 +83,7 @@ public static boolean percentageChance(int chance, Random random) {

public static boolean fractionChance(int denominator) {
Validate.isPositive(denominator);
return nextInt(0, denominator) == 0;
return RANDOM.nextInt(denominator) == 0;
}

public static boolean fractionChance(int denominator, Random random) {
Expand All @@ -91,7 +93,7 @@ public static boolean fractionChance(int denominator, Random random) {

public static boolean fractionChance(int numerator, int denominator) {
Validate.isFractionChance(numerator, denominator);
return nextInt(0, denominator) < numerator;
return RANDOM.nextInt(denominator) < numerator;
}

public static boolean fractionChance(int numerator, int denominator, Random random) {
Expand Down

0 comments on commit cc5fa25

Please sign in to comment.