HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Generating random numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomgeneratingnumbers

Problem

I'm trying to refactor the following code and could use some suggestions. Basically, three functions generating random numbers in different ways are called:

import java.util.Random;
public final class JP_RandomNumbers {

    /**
     * @param aArgs
     */

    public static void main(String[] aArgs) {
        genRandomNumbers1();
        genRandomNumbers2();
        genRandomNumbers3();
    }

    private static void genRandomNumbers1() {
        log("Generating 10 random integers in range 0..99.");

        //note that a single Random object is reused here
        Random randomGenerator = new Random();
        for (int idx = 0; idx  aEnd) throw new IllegalArgumentException("Start cannot exceed End.");

        //get the range, casting to avoid overflow problems
        long range = (long)aEnd - (long) aStart + 1;
        //compute a fraction of the range , 0 <= frac <= range
        long fraction = (long) (range * aRandom.nextDouble());
        int randomNumber = (int)(fraction + aStart);
        log("Generated : " + randomNumber);
    }

    private  void genRandomNumbers3() { 
        double MEAN = 100.0f;
        double VARIANCE = 5.0f;
        for(int idx = 1; idx <= 10; ++idx){
            log("Generated : " + String.valueOf(getGaussean(MEAN,VARIANCE)));       
        }
        log ("Done");

    }

    private double getGaussean(double aMean, double aVariance){
        return aMean + fRandom.nextGaussian() * aVariance;
    }

    //Private 
    private static final int RANGE = 10;
    private Random fRandom = new Random();
    private static void log( String aString) { 
        System.out.println(aString);
    }

}


Output:

```
Generating 10 random integers in range 0..99.
Generated :74
Generated :99
Generated :75
Generated :15
Generated :32
Generated :31
Generated :96
Generated :61
Generated :80
Generated :89
Done.
Generated : 2
Generated : 4
Generated : 1
Generated : 9
Generated : 9
Generated : 5
Generated : 9
Generated : 4
Generated : 3
Gener

Solution

The number generation technique is generally sound. I just have the following minor suggestions:

-
The idiomatic way to repeat something 10 times is

for (int idx = 0 ; idx < 10 ; ++idx) { … }


-
Use the fRandom instance variable; you shouldn't need to create new Random() in each function.

  • You hard-coded values in each function; those constants should probably be pulled out to the class level.



  • showRandomInteger() should be changed to return a random value instead of logging it.



-
The compiler will implicitly do String.valueOf() for you, so you can just write

log("Generated : " + getGaussean(MEAN,VARIANCE));


  • getGaussean() should be spelled getGaussian().

Code Snippets

for (int idx = 0 ; idx < 10 ; ++idx) { … }
log("Generated : " + getGaussean(MEAN,VARIANCE));

Context

StackExchange Code Review Q#38297, answer score: 5

Revisions (0)

No revisions yet.