## 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

## 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.doubles():

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

`randomNumbers.doubles();`

ints():

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

`randomNumbers.ints();`

longs():

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

`randomNumbers.longs();`

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

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

GO TO FULL VERSION