CodeGym/Java Blog/Java Classes/Java.util.Random class
Author
Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

Java.util.Random class

Published in the Java Classes group
members

What is java.util.Random class in Java?

The java.util.Random class is used to generate pseudorandom numbers. The methods implemented by this class are used to generate different random data types such as int, double, and float. So there are two major types of random numbers, statistically random and pseudorandom numbers. The statistically random numbers (like simple random numbers in maths), are a set of values that do not have a recognizable pattern. For example, rolling a dice 10 times will generate a random number each time that does not have any recognizable pattern.

What are Pseudo Random Numbers?

It is a set of values that are statistically random but have a known starting point. In Java, each pseudorandom number is produced through an algorithm. So typically a cycle is repeated every time java.util.Random generates a random number. A random number generator in Java will produce statistically random numbers but with a known starting point, generated by an algorithm. That makes those values pseudorandom.

Is java.util.Random class Cryptographically Insecure?

It is cryptographically insecure because it has algorithms implemented for generating random numbers. As a result of which a person knowing how the algorithm works will not take much time accessing your sensitive data. So if you are working with some security applications, needed to secure some sensitive data, or need to generate random passwords then it's better to avoid using this class. It is helpful in many cases like generating a random number for your dice roll in ludo, gambling, doing a toss for a match or other areas where your desired result is unpredictable.

Method Declaration

For using the methods of this class you need to import it first from the package as
import java.util.Random;
After importing, you need to create an object of this class like
Random randomNumbers = new Random();
Moving further, let’s have a look at different examples to use the java.util.Random class.

Example

import java.util.Random;

public class RandomClassExample1 {

	public static void main(String[] args) {

		// create random object
		Random randomNumbers = new Random();

		System.out.println("----------Random Boolean---------" );
		/*
		 * Returns the next pseudo random boolean value which
		 * may be used in a toss for a match
		 */
		boolean value = randomNumbers.nextBoolean();
		System.out.println("The random boolean value is: " + value);

		/*
		 * Returns the next pseudo random integer value between 0 and 5
		 * because if we use '6' then, it will give random numbers from 0 to 6
		 * hence incrementing it by 1 you can use it as a result of a dice roll
		 */
		System.out.println("\n----------Random Integer---------" );
		System.out.println("Die Roll: " + (randomNumbers.nextInt(6)+1) );
		System.out.println("Die Roll: " + (randomNumbers.nextInt(6)+1) );
		System.out.println("Die Roll: " + (randomNumbers.nextInt(6)+1) );
		System.out.println("Die Roll: " + (randomNumbers.nextInt(6)+1) );
		System.out.println("Die Roll: " + (randomNumbers.nextInt(6)+1) );

		// return the next pseudo random long value
		Long val = randomNumbers.nextLong();
		System.out.println("\n----------Random Long---------" );
		System.out.println("Random Long value: " + val);

		/*
		 * Generates random bytes and puts them in an array, which you can for some
		 * desired unpredictable result that is summing all the values in the array
		 */

		System.out.println("\n----------Random Bytes---------" );
		byte[] bytes = new byte[8];
		randomNumbers.nextBytes(bytes);

		System.out.print("These are the random bytes = [ ");
		for (int i = 0; i < bytes.length; i++) {
			System.out.printf("%d ", bytes[i]);
		}
		System.out.println("]");
	}

}

Output

----------Random Boolean--------- The random boolean value is: true ----------Random Integer--------- Die Roll: 4 Die Roll: 6 Die Roll: 1 Die Roll: 1 Die Roll: 3 ----------Random Long--------- Random Long value: -6029959293504570824 ----------Random Bytes--------- These are the random bytes = [ -37 90 -98 -70 23 -111 19 108 ]

Methods

Now we will discuss some of the methods provided by this class, keeping in mind that the randomNumbers is the object or instance of our class.
  1. doubles():

    This method returns an infinite series of pseudorandomly generated double values. Now let's see how to use this method.

    randomNumbers.doubles();
  2. ints():

    This method returns an infinite series of pseudorandomly generated integer values. You can use the following script to call this method.

    randomNumbers.ints();
  3. longs():

    This method returns an infinite series of pseudorandomly generated long values. The following script is used to call this method.

    randomNumbers.longs();
  4. nextBoolean():

    This method returns the next uniformly distributed pseudorandom boolean value from the random numbers generator sequence. The uniform distribution is rectangular-shaped, meaning every value in the distribution has an equal probability. Here’s how to use this method.

    randomNumbers.nextBoolean();
  5. nextInt(int n):

    This method returns the next uniformly distributed pseudorandom integer value from the random numbers generator sequence between 0 inclusive and the provided value exclusive. Have a quick view of how to use this method.

    randomNumbers.nextInt(10);

Conclusion

We hope by now you understand what is java.util.Random and how to implement its different methods and in which scenario to use it. You can always use this class in java. Feel free to practice and get back here whenever you need more assistance. Happy learning!
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet