Random class in java

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 Methods Defined by Random

 Method Description  
 boolean nextBoolean( ) Returns the next boolean random number. 
 void nextBytes(byte vals[ ]) Fills vals with randomly generated values. 
 double nextDouble( ) Returns the next double random number. 
 float nextFloat( ) Returns the next float random number. 
 double nextGaussian( ) Returns the next Gaussian random number. 
 int nextInt( ) Returns the next int random number. 
 int nextInt(int n) Returns the next int random number within the range zero to n. 
 long nextLong( ) Returns the next long random number. 
 void setSeed(long newSeed)Sets the seed value (that is, the starting point for the random 
   number generator) to that specified by newSeed. 

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) {





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("*");






Average of values: 0.0702235271133344











Leave a Comment