Skip to content

Commit

Permalink
Add simple thread random getter
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Aug 26, 2023
1 parent f9a2fd0 commit 095dd3f
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions reactions/src/main/java/fun/reactions/util/Rng.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fun.reactions.util;

import fun.reactions.util.NumberUtils.Is;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.random.RandomGenerator;

public final class Rng {
private Rng() {}
Expand All @@ -13,35 +15,35 @@ public static <T> T randomElement(List<T> list) {
}

public static boolean percentChance(double chance) {
return Rng.nextDouble(100) < chance;
return nextDouble(100) < chance;
}

public static boolean chance(double chance) {
return nextDouble() < chance;
}

public static boolean nextBoolean() {
return ThreadLocalRandom.current().nextBoolean();
return threadRandom().nextBoolean();
}

public static int nextInt(int max) {
return ThreadLocalRandom.current().nextInt(max);
return threadRandom().nextInt(max);
}

public static int nextInt(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max);
return threadRandom().nextInt(min, max);
}

public static double nextDouble() {
return ThreadLocalRandom.current().nextDouble();
return threadRandom().nextDouble();
}

public static double nextDouble(double max) {
return ThreadLocalRandom.current().nextDouble(max);
return threadRandom().nextDouble(max);
}

public static double nextDouble(double min, double max) {
return ThreadLocalRandom.current().nextDouble(min, max);
return threadRandom().nextDouble(min, max);
}

/**
Expand All @@ -67,4 +69,8 @@ public static int nextIntRanged(String numsStr) { // TODO Requires refactoring t
}
return NumberUtils.asInteger(numsStr);
}

public static @NotNull RandomGenerator threadRandom() {
return ThreadLocalRandom.current();
}
}

0 comments on commit 095dd3f

Please sign in to comment.