user2736224
user2736224

Reputation: 91

Java - Implementing a random vector generator that produces normally distributed numbers

I'm attempting to implement a random vector generator in Java that produces normally distributed double values as part of a Monte Carlo simulation. So far, my approach has been to write a class, GaussianRandomNumberGenerator, that uses the apache commoms library to build its namesake object with 2 key inputs, vector length and a RandomGenerator.

package mcPkg;

import org.apache.commons.math3.random.RandomGenerator;

public abstract class GaussianRandomNumberGenerator implements RandomVectorGenerator{

    private int N;
    private RandomGenerator rg;

    public GaussianRandomNumberGenerator(int N, RandomGenerator rg){
        this.N = N;
        this.rg = rg;
    }

    public double nextNormalizedDouble() {
        return rg.nextGaussian();
    }

    @Override
    public double[] getVector() {
        double[] vector = new double[N];
        for ( int i = 0; i < vector.length; ++i){
            vector[i] = nextNormalizedDouble();
        }
        return vector;
    }

}

The class should then utilize an interface, RandomVectorGenerator, in the main program (I will need the interface for additional tasks with other classes):

package mcPkg;

public interface RandomVectorGenerator {

    public double[] getVector();

}

Unfortunately, when I attempt to run a test in the main program, I get an error saying:

Cannot instantiate the type GaussianRandomNumberGenerator

with regards to the local variable grng (see code below):

package mcPkg;

import org.apache.commons.math3.random.GaussianRandomGenerator;
import org.apache.commons.math3.random.RandomGenerator;

import mcPkg.GaussianRandomNumberGenerator;
import mcPkg.RandomVectorGenerator;

public class MonteCarloTest {

    public static void main(String[] args) {
        RandomGenerator rg;
        GaussianRandomNumberGenerator grng = new GaussianRandomNumberGenerator(10, rg);
        grng.getVector();
    }

}

Any insight on why this is happening, as well as suggestions for an efficient solution to the issue, would be greatly appreciated. Thanks in advance!

Upvotes: 0

Views: 2107

Answers (1)

Krayo
Krayo

Reputation: 2510

I think the abstract keyword is the problem. I suggest that remove it from the GaussianRandomNumberGenerator class.

Upvotes: 2

Related Questions