Library RandomXT

Discussion in 'Resources' started by Gorbit99, Dec 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Gorbit99

    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!
     
    Last edited: Dec 29, 2015
  2. Offline

    teej107

    Use ThreadLocalRandom or create a single static Random constant.
     
    Mrs. bwfctower likes this.
  3. Offline

    Gorbit99

    teej107 likes this.
  4. Offline

    KingFaris11

    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
     
  5. Offline

    mcdorli

    The first article here
     
    teej107 and KingFaris11 like this.
  6. Offline

    Gorbit99

    @teej107 BTW, creating a single static Random wouldn't be as easy, because I don't plan on having a constructor
     
  7. Offline

    teej107

    @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.
     
    Mrs. bwfctower and Gorbit99 like this.
Thread Status:
Not open for further replies.

Share This Page