Class random in java
The Random class is a pseudo-random number generator. Pseudo-random numbers are generated by starting with a seed value and then using an algorithm to generate a sequence of numbers that appear to be random. The Random class uses a 48-bit seed and a linear congruential algorithm to modify the seed.
As a consequence of this implementation, two Random instances that are constructed with the same seed value generate exactly the same sequence of numbers.
The Random class provides methods that return pseudo-random values for various primitive Java types. The Math.random() method is easier to use if we do not need to fine-tune the generation of random numbers.
For an 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 a method, calls are made from each, hence they will generate and return an 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 the 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 the method next(int)
.
The invocation new Random(seed)
is equivalent to:
The class structure of Random is given as:
public class java.util.Random extends java.lang.Object implements java.io.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);
}
The details of the class structure are given as follows:
public Random();
public Random() constructor creates a Random object with the current time as its seed value.
public Random(long seed);
public Random(long seed) constructor creates a Random object with the given seed value.
Parameter
seed – The seed value to use.
public void nextBytes(byte[] bytes);
public void nextBytes(byte[] bytes); method fills the given array with pseudo-random byte values.
Parameter
bytes – The byte array to fill.
public double nextDouble();
public double nextDouble() method returns the next pseudo-random, uniformly distributed double value. The value is
between 0.0 and 1.0 inclusive.
This method returns the next pseudo-random double value.
public float nextFloat();
public float nextFloat() method returns the next pseudo-random, uniformly distributed float value. The value is
between 0.0 and 1.0 inclusive.
This method returns the next pseudo-random float value.
public synchronized double nextGaussian();
public synchronized double nextGaussian() method returns the next pseudo-random, Gaussian distributed double value. The value has a mean of 0.0 and a standard deviation of 1.0 from the random number sequence. The value is between -1.0 and 1.0.
This method returns the next pseudo-random double value.
public int nextInt();
public int nextInt() method returns the next pseudo-random, uniformly distributed int value.
This method returns the next pseudo-random int value.
public long nextLong();
public long nextLong() method returns the next pseudo-random, uniformly distributed long value.
This method returns the next pseudo-random long value.
public synchronized void setSeed(long seed);
public synchronized void setSeed(long seed) method sets this Random object’s seed value to the given value.
Parameter
seed – The new seed value.
protected synchronized int next(int bits);
protected synchronized int next(int bits) method generates the given number of bits and returns them as an integer value. A subclass of Random should override this method, as it is used by all of the other random-number generation
methods in the class.
This method returns the specified number of pseudo-random bits.
Parameter
bits – The number of bits to generate.
Apart from these Random class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)