Bivariate Gaussian Generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grabonlee
    New Member
    • Jul 2007
    • 8

    #1

    Bivariate Gaussian Generator

    Hi

    Please take a look at the code below. I'm trying to generate an array of random numbers with mean(1.0,1.0) and covariance(1.0, 0.0,0.0,1.0).

    I'm stuck with how to make the number of samples(random numbers) up 10000.


    import java.util.Rando m;

    public class BivariateGaussi anGenerator
    {

    static float mu1,mu2,co1,co2 ,co3,co4,covari ance1,
    covariance2,cov ariance3,covari ance4,mean1,mea n2;



    public static void setMean(float mu1,float mu2 )
    {
    mean1=mu1;
    mean2=mu2;
    }

    public void setCovariance(f loat co1,float co2,float co3,float co4)
    {
    covariance1=co1 ;
    covariance2=co2 ;
    covariance3=co3 ;
    covariance4=co4 ;
    }

    public static float[] generateRandom( )
    {
    int count = 100000;

    Random generate = new Random();
    float[] genNumbers = new float[2];


    for(int i=0;i<count;i++ )
    {
    genNumbers[i] = Math.abs((float )generate.nextG aussian());
    }
    return genNumbers;
    }

    public static void main(String[] args)

    {

    BivariateGaussi anGenerator bgg = new BivariateGaussi anGenerator();

    bgg.setMean(1.0 f,1.0f);
    bgg.setCovarian ce(1.0f,0.0f,0. 0f,1.0f);

    float[] x = bgg.generateRan dom();


    System.out.prin tln("["+x[0]+" , " +x[1]+"]");

    }

    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You have your indexes all messed up, i.e. you define an array that can hold
    two floating point numbers and you try to stick 100,000 N(0,1) numbers in it;
    that will throws an ArrayIndexOutOf BoundsException .

    kind regards,

    Jos

    Comment

    • grabonlee
      New Member
      • Jul 2007
      • 8

      #3
      Originally posted by JosAH
      You have your indexes all messed up, i.e. you define an array that can hold
      two floating point numbers and you try to stick 100,000 N(0,1) numbers in it;
      that will throws an ArrayIndexOutOf BoundsException .

      kind regards,

      Jos
      Thanks

      Yes I want to create array.length = 2. I also want each array to generate up 100,000 random numbers, which would be bound by a defined mean and covariance.
      I want to also plot a graph. Like in Matlab, the more the random samples, the clearer the graph. What can I do?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by grabonlee
        Thanks

        Yes I want to create array.length = 2. I also want each array to generate up 100,000 random numbers, which would be bound by a defined mean and covariance.
        I want to also plot a graph. Like in Matlab, the more the random samples, the clearer the graph. What can I do?
        Didn't that method I copied from Abramowitz and Stegun help you? See your
        other thread for that.

        kind regards,

        Jos

        Comment

        Working...