All About Class Random in Java

0
33
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

 

All About Class Random in JavaAll About Class Random in Java

All about class random in java. For example of this class Random in java  is used to generate a stream of pseudo random numbers.

However 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 from each, hence they will generate and return identical sequence of numbers.

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each.

They will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random.

Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code.

However, subclasses of class Random are permitted to use other algorithms. So long as they adhere to the general contracts for all the methods.

Create a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

The invocation new Random(seed) is equivalent to:

All About Class Random in Java

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

}

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here