1

I need to generate random numbers in my code but I want to change the parameters of the Distribution based on the current scenario. The application can run as single or multi-threaded application.

My question is that, should I initialize the RandomGenerator object in the constructor of my class and then use that RandomGenerator object to (re-)initialize the NormalDistribution, BetaDistribution or any other object of AbstractRealDistribution repeatedly, or just initialize my distribution object after I need to update the parameters.

Which is is a better option in terms of generating good random numbers and also in terms of optimality?

Case 1:

class Test {
    protected RandomGenerator rng;
    public Test() {
        rng = new Well19937c();
    }
    private void someFunction(double mean, doube std_dev) {
        NormalDistribution norm = new NormalDistribution(this.rng, mean, std_dev);
        while (condition is met) {
            // do some calculation, create some random numbers, get new mean and std_dev
            norm = new NormalDistribution(this.rng, new_mean, new_std_dev);
        }
    }
}

Case 2:

class Test {
    private void someFunction(double mean, doube std_dev) {
        NormalDistribution norm = new NormalDistribution(mean, std_dev);
        while (condition is met) {
            // do some calculation, create some random numbers, get new mean and std_dev
            norm = new NormalDistribution(new_mean, new_std_dev);
        }
    }
}
Atif
  • 345
  • 1
  • 4
  • 16

1 Answers1

0

You should reuse the RandomGenerator across calls. The simplest way to generate random values with different distribution parameters is to use the RandomDataGenerator class in the random package:

RandomDataGenerator generator = new RandomDataGenerator(new Well19937c());
// Unit normal
double normDev = generator.nextGaussian(0, 1);
// mean = 0.5, std dev = 2
double normDev2 = generator.nextGaussian(0.5, 2);
// exponential, mean = 1
double expDev = generator.nextExponential(1);
Phil Steitz
  • 644
  • 3
  • 10
  • I did find a similar reference [here](http://commons.apache.org/proper/commons-math/userguide/random.html). I have a question about using `RandomDataGenerator`, if I use a `RandomGenerator` and use an `AbstractRealDistribution` object with the `RandomGenerator`, then I can avoid lots of `if... else` checks because I can initailize the `AbstractRealDistribution` object once using the required distribution at startup and then I can get the data using `rand.sample()` without using conditional checking in each function call, which could go to hundreds of thousands or millions. Any suggestions? – Atif Apr 14 '15 at 17:38
  • If you use a `RandomDataGenerator`, it will take care of creating distribution instances as needed. You don't need to directly instantiate any distribution instances. If you want to instantiate distributions, you can do that and just use the sample() method on the distribution instance. The key is to make sure that you reuse the `RandomGenerator.` Note that the distribution classes are immutable, though; so you can't change their parameters (as you can change the actual parameters in the nextXxx calls to a `RandomDataGenerator`). – Phil Steitz Apr 15 '15 at 02:38