1

I cannot figure out why the r array will not sort into ascending order. I have tried Array.sort and manually sorting the array.

    import java.lang.*;
    import java.lang.Object;
    import java.lang.Integer;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.GregorianCalendar;

    public class Gaussian {
      public static int seed;
      public static final int n = 100;
      public static void main(String argv[]) { 
        double r[] = new double[100];

     // Initiate the seed from the current time
        GregorianCalendar t = new GregorianCalendar();
        int t1 = t.get(Calendar.SECOND);
        int t2 = t.get(Calendar.MINUTE);
        int t3 = t.get(Calendar.HOUR_OF_DAY);
        int t4 = t.get(Calendar.DAY_OF_MONTH);
        int t5 = t.get(Calendar.MONTH);
        int t6 = t.get(Calendar.YEAR);
        seed = t6 + 65*(t5+12*(t4+31*(t3+24*(t2+60*t1))));
        if ((seed%2) == 0) seed = seed-1;

**************This is the section giving me trouble*****************

// Put the Gaussian random numbers in the array
    for (int i=0; i<n-1; i+=1) {
      r = rang();
      for (int l=0; l<r.length-1; l++) {  
          if(r[l] < r[l+1]) {
             double tempValue = r[l+1];
             r[l+1] = r[l];
             r[l] = tempValue;

          }
      }
      System.out.println(r[0]);

******************Between these stars*******************************

    }
  }

// Method to create two Gaussian random numbers from two
// uniform random numbers in [0,1].

  public static double[] rang() {
    double x[] = new double[1];
    double r1, r2;
    r1 = - Math.log(1-ranf());
    r2 = 2*Math.PI*ranf();
    r1 = Math.sqrt(2*r1);
    x[0] = r1*Math.cos(r2);        
    return x;
  }

// Method to generate a uniform random number in [0,1]
// following x(i+1)=a*x(i) mod c with a=pow(7,5) and
// c=pow(2,31)-1. Here the seed is a global variable.

  public static double ranf() {
    final int a = 16807, c = 2147483647, q = 127773,
      r = 2836;
    final double cd = c;
    int h = seed/q;
    int l = seed%q;
    int t = a*l-r*h;
    if (t > 0) seed = t;
    else seed = c+t;
    return seed/cd;
  }

}

For some reason it is giving me this output:

-0.7286443240313888
0.9205595151394262
-0.1201523471771766
-0.2955395834645375
0.5562293071303744
0.5947383124976592
-0.5190410499731951
1.1878905341959594
-0.6530738641804281
1.92941716216534
-1.55458771926982
1.011542837179014
-1.2973072313970084
-0.5115664645635027
-0.4537839981939878
-0.43386113937789456
2.1877083571592637
-0.1869725174568339
1.0427194985616417
0.7491392218512473
-0.2837863829399006
0.09204148771478798
0.08980225475596745
-1.0595943397788652
0.2493101533697332
-1.3926086961785766
0.9722238128294852
0.4490619874581054
1.4379635505387074
1.4550206564181973
-0.9754513444753741
-1.6454765651087158
0.1683214049373476
0.9981636099004854
-0.7289169766110786
1.6612385375332162
0.19025688479326378
0.0830947016802825
0.4674778575126086
-0.9077431792737849
-0.5638299547034225
0.13229202082089384
1.2429372493642745
-0.006685432080368285
2.336192098747748
-0.5450098522726261
-1.6420372037670146
0.3400579125911062
0.48689741262698993
-0.5075527810259783
1.9679760629290328
-1.9114919760463223
0.5633783650935041
0.12871665512520616
-1.8826404473732248
0.16725744941405607
1.049647212107755
0.767071049830706
0.3366261688045942
-1.726395330988362
-0.15241706234915703
-0.17645549457761323
1.098469368528642
-0.3173352964219553
-2.6584067823396675
0.4136264577634812
-1.2506808927401905
2.0462718170224186
-2.380899123430688
-1.0340941198026203
-3.223035072470854
-0.1423807157151033
-0.048464495873010084
1.4690537691472332
0.9110766995396362
-0.040683539673625924
-0.3895836309957472
-0.4793889976948361
0.007621022168540105
0.4151797552606307
1.2734508381903344
0.6398148976757589
-2.0458807284022114
0.23937728902415573
0.09380205942857836
1.331532378407905
-0.09813530948364875
0.9515533393393638
-1.5924626733327882
-1.2504131049626441
0.3674623983411812
0.8204457493547238
0.2214473639135442
0.5573901544532469
1.6349106235864332
-1.4373482822115147
0.38216369985059967
-0.6869980429363977
0.30632157455967757

Instead of sorting the numbers in ascending order.

4
  • 1
    Your r array only has one element when you try to sort it. How do you sort an array with 1 element? In any case, your sorting algorithm wouldn't sort correctly. Commented Sep 21, 2017 at 21:06
  • When I move r = rang(); out of the loop it only returns one value :( Commented Sep 21, 2017 at 21:07
  • Ohhhh, so it's not reading all the rang() values into r? Commented Sep 21, 2017 at 21:07
  • double r[] = new double[100] Value of r is now a reference to an array of 100 zeroes. --- r = rang(); Value of r is now a reference to an array of 1 value, since that is what rang() returns. Original array of 100 zeroes is discarded. Commented Sep 21, 2017 at 21:15

2 Answers 2

1

The reason the the array is not sorting is because of r = rang();. Initially, double r[] = new double[100]; sets r to an array of length 100. However, r is being reassigned to the result of rang(), which is an array the length of 1. This causes the inner for loop to never run.

I recommend the following changes:

// I'm assuming that you want 100 Gaussian random numbers, not 99
for (int i = 0; i < r.length; i += 1) {
    // you could also change rang() to return a 
    // double instead of an array of double 
    // because the array only contains 1 double
    double[] randomGaussian = rang();
    r[i] = randomGaussian[0];
}

// sort the array
Arrays.sort(r);

// print out the array
for(int i = 0; i < r.length; i++) {
    System.out.print(r[i] + " ");
}
Sign up to request clarification or add additional context in comments.

Comments

0

I figured it out guys!

// Put the Gaussian random numbers in the array
    double copy[] = new double[n];
    for (int i=0; i<n-1; i+=1) {
      r = rang();
      double temp = r[0];
      copy[i] = temp;

    }

// Sort the array
    Arrays.sort(copy);
    for (int i=0; i<n-1; i++) {
    System.out.println(copy[i]); 
    }

Gives the right output!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.