Spread the love

Class Random in Java

Spread the love

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.

READ  Abstract Class DateFormat in Java

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.

READ  Abstract Class DateFormat in Java

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.

READ  Class IndexOutOfboundException in Java

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.

READ  Class InvalidClassException in Java

This method returns the specified number of pseudo-random bits.

Parameter
bits – The number of bits to generate.

READ  What Is Casting Upcasting and Downcasting in Java?

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)




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Comments

  1. When I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I get 4 emails with the exact same comment. There has to be a means you can remove me from that service? Thanks!

  2. Next time I read a blog, Hopefully it won’t fail me as much as this one. After all, Yes, it was my choice to read, nonetheless I truly thought you’d have something helpful to say. All I hear is a bunch of crying about something that you could possibly fix if you were not too busy seeking attention.

  3. I can write better than the average person (not that that is saying much these days) and I have strong opinions on various subjects. I don’t have experience as a journalist or technical writer, but what does a person need to start blogging and who pays, if the blogger gets picked up as a writer?.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love