Java2all.com  

Random class





Share This Topic -

Random class :

  • The Random class allows you to generate random double, float, int, or long numbers.
  • This can be very helpful if you are building a simulation of a real-world system.
  • This class provides the following constructors.

            Random()

            Random(long start)

Here, start is a value to initialize the random number generator.

Method

Description

Double nextDouble()

Returns a random double value.

Float nextFloat()

Returns a random float value.

Double nextGaussian()

Returns a random double value. Numbers obtained from repeated calls to this method have a Gaussian distribution with a mean of 0 and a standard deviation of 1.

Int nextInt()

Returns a random int value.

Long nextLong()

Returns a random long value.

EX :

 import java.util.*;
public class Random_Demo
{
	public static void main(String args[])
	{
		Random ran = new Random();
		for(int i = 0; i<5;i++)
		{
				System.out.println(ran.nextInt());
		}
	}
}

 
Output:
 
64256704
1265771787
-1962940029
1372052