All About Class Random in Java





An instance of this class Random is used to generate a stream of pseudo random numbers.The class uses a 48-bit seed,which is modified using a linear congruent formula.If two instances of Random are created with the same seed and the same sequence of method calls are made fro each,they will generate and return identical sequence of numbers.

The class structure of Random  is given as:


public class java.util.Random extends java.lang.Object implements Serializable{
//constructors
public Random()//creates a new Random number generator.Its seed is initialized to a value based on the current time.
public Random(long seed)//creates a new Random number generator using a long seed.
//methods
public double nextDouble();//returns values between 0.0 to 1.0.
public int nextInt();//nextInt() generates int values through out the range of this data types.
public float nextFloat();//returns values between 0.0 to 1.0.
public long nextLong();//nextLong() generates long values through out the range of this data types.
public void nextBytes(byte[] bytes);
public synchronized double nextGaussian();//returns the pseudo random values with Gaussian distribution.
//the mean value is 0.0 and standard deviation is 1.0 
public synchronized void setSeed(long seed);// this helps to initialize the pseudo random number generator with
//some  variable seed (not the current time- it is default though) or with a constant to generate repeatable 
//sequence of pseudo-random numbers
protected synchronized int next(int bits);

}
 
All About Class Random in Java All About Class Random in Java Reviewed by Animesh Chatterjee on October 04, 2018 Rating: 5

No comments:

Powered by Blogger.