Class Random in Java

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.

Ads code goes here

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  Dangling Else Problem 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.

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 StringWriter 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.

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

Parameter
bits – The number of bits to generate.

READ  Abstract Class SocketImpl 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)

Share and Enjoy !

22 thoughts on “Class Random in Java”

  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?.

  4. Wonderful items from you, man. I’ve take into accout your stuff previous to and you’re simply too great. I actually like what you’ve got right here, certainly like what you’re stating and the way in which you assert it. You are making it entertaining and you continue to care for to stay it smart. I cant wait to learn far more from you. That is actually a wonderful website.

  5. I just want to say I am new to blogging and site-building and really savored your web page. Almost certainly I’m planning to bookmark your website . You certainly have good well written articles. With thanks for sharing with us your blog.

  6. Helpful piece – people need to see more like this, because most info on this topic is done-to-death. You provide real value to your readers.

  7. This is great! I’m glad I found your post because it’s an improvement on similar blogs I’ve seen from most other bloggers on this subject. Can I ask you to elaborate? Could you write any further example? many thanks 🙂

  8. Good post – people need to read more like this, as most info about this topic is done-to-death. You provide real worth to people.

  9. Hello! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

Leave a Comment

Your email address will not be published. Required fields are marked *