Programming Tutorials

Random class sample program in Java - nextGaussian(), nextBoolean(), nextBytes(), nextInt(), nextLong(), nextFloat(), nextDouble()

By: Manoj Kumar in Java Tutorials on 2007-09-14  

The Random class is a generator of pseudorandom numbers. These are called pseudorandom numbers because they are simply uniformly distributed sequences. Random defines the following constructors:

Random()
Random(long seed)

The first version creates a number generator that uses the current time as the starting, or seed, value. The second form allows you to specify a seed value manually.

If you initialize a Random object with a seed, you define the starting point for the random sequence. If you use the same seed to initialize another Random object, you will extract the same random sequence. If you want to generate different sequences, specify different seed values. The easiest way to do this is to use the current time to seed a Random object. This approach reduces the possibility of getting repeated sequences.

There are six types of random numbers that you can extract from a Random object. Random Boolean values are available from nextBoolean(). Random bytes can be obtained by calling nextBytes(). Integers can be extracted via the nextInt() method. Long integers, uniformly distributed over their range, can be obtained with nextLong(). The nextFloat() and nextDouble() methods return a uniformly distributed float and double, respectively, between 0.0 and 1.0. Finally, nextGaussian() returns a double value centered at 0.0 with a standard deviation of 1.0. This is what is known as a bell curve.

Here is an example that demonstrates the sequence produced by nextGaussian(). It obtains 100 random Gaussian values and averages these values. The program also counts the number of values that fall within two standard deviations, plus or minus, using increments of 0.5 for each category. The result is graphically displayed sideways on the screen.

// Demonstrate random Gaussian values.
import java.util.Random;

class RandDemo {
    public static void main(String args[]) {
        Random r = new Random();
        double val;
        double sum = 0;
        int bell[] = new int[10];
        for (int i = 0; i < 100; i++) {
            val = r.nextGaussian();
            sum += val;
            double t = -2;
            for (int x = 0; x < 10; x++, t += 0.5)
                if (val < t) {
                    bell[x]++;
                    break;
                }
        }
        System.out.println("Average of values: " +
                (sum / 100));
        // display bell curve, sideways
        for (int i = 0; i < 10; i++) {
            for (int x = bell[i]; x > 0; x--)
                System.out.print("*");
            System.out.println();
        }
    }
}

Here is a sample program run. As you can see, a bell-like distribution of numbers is obtained. (Your output may look a bit different since we are using Random.)

Average of values: 0.04895742052535768
***
****
********        
****************
**********************
**************
*************
************
******
*





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)