When I was programming, I was running into some problems with Random. Sometimes it didn't have things I needed and when I made them myself, they took an awful lot of space to make, so I created this library. What it does: It has some features that the original Random class doesn't, for example bellcurves and dice throws. Methods: RandomXT.bellCurve(int steepness, int limit); this method gives you a weighted value between 0(inclusive) and "limit"(exclusive). steepness: The steepness of the bell curve. Value must be at least 2.RandomXT.cappedDecimal(int decimalCount, float limit); Gives you a random float with maximum "decimalCount" decimals. limit: Maximum value of random(exclusive).RandomXT.diceThrow(int sides, int dices); returns a list of ints with "dices" ints inside with a minimum value of 1 (inclusive) and maximum of "sides" (inclusive) Code: Code: package libs; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class RandomXT { public static int bellCurve(int steepness, int limit) { int sum = 0; for (int x = 0; x < steepness; x++) { sum += ThreadLocalRandom.current().nextInt(limit); } int rand = sum / steepness; return rand; } public static float cappedDecimal(int decimalCount, float limit) { float rand = ThreadLocalRandom.current().nextInt((int) Math.round(limit * Math.pow(10, decimalCount))); rand /= Math.pow(10, decimalCount); return rand; } public List<Integer> diceThrow(int sides, int dices) { List<Integer> thrown = new ArrayList<Integer>(); for (int x = 0; x < dices; x++) { thrown.add(ThreadLocalRandom.current().nextInt(sides) + 1); } return thrown; } } Write down your suggestions down below!
Can you explain this? Normally I'd have a Utilities class and have: Code: private static Random random = new Random(); public static Random getRandom() { return random; } What does ThreadLocalRandom do? o.o
@teej107 BTW, creating a single static Random wouldn't be as easy, because I don't plan on having a constructor
@Gorbit99 You don't need a constructor to declare and assign values to static fields. Code: private static final Random RANDOM = new Random(); ThreadLocalRandom is a good one to use though.