Jump to content

grandom() deprecated?


Recommended Posts

Posted (edited)

Cruising the API and I see that grandom is deprecated. Does random offer a similar functionality considering standard deviation etc? Or is there some other function I should be using for similar results?

 

EDIT: Perhaps I should use this:

 

Edited by avid
Posted

I deprecated it because it doesn't create a normal distribution, it only creates the positive half of the bell curve. My official solution was/is the following:

 

 public static int gRandom(int mean, double stdDeviation)
            throws IllegalArgumentException {
        if (mean < 0)
            throw new IllegalArgumentException("Mean can't be lower than 0!");
        double max = stdDeviation * 3;
        int result;
        do {
            result = Math.toIntExact(Math.round(random.nextGaussian() * stdDeviation + mean));
        } while (Math.abs(result - mean) > max);
        return result;
    }

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...