*1.2K*

Table of Contents

In Java, various random numbers exist, including integer, float, double, long, and boolean, created within a given range. In Java, there are four techniques for generating random numbers.

### Random Class

The first method is to use a random class. Follow the steps below to utilize the Random Class to generate random numbers:

- Import the java.util.Random class
- Creating a new Random instance class by typing Random rand = new. Random()
- Invoke one of the following rand object methods:
- Int(upperbound) returns a random number between 0 and upperbound-1.next
- Float() returns a float with a value between 0.0 and 1.0.
- Double() returns a double with a value between 0.0 and 1.0.

## Generating random numbers in Java with examples

import java.util.Random; class RandomNumberGeneration { public static void main( String args[] ) { Random rand_val = new Random(); //instance of random class int upperbound = 30; //generating random values from 0-29 int rand_integer = rand.nextInt(upperbound); double rand_double=rand_val .nextDouble(); float rand_float=rand_val .nextFloat(); System.out.println("Random integers from 0 to" + (upperbound-1) + " : "+ rand_integer ); System.out.println("Random float value between 0.0 and 1.0 : "+float_random); System.out.println("Random double value between 0.0 and 1.0 : "+rand_double); } }

### Use ThreadLocalRandom to generate Random Numbers

Follow the instructions below to create random numbers using the ThreadLocalRandom class:

- Import the java.util.concurrent class
- ThreadLocalRandom – Invoke the method.
- ThreadLocalRandom.current().next is used to create random numbers of type int – Int()
- ThreadLocalRandom.current.next uses Int() to generate a random number of type double () – Double()
- ThreadLocalRandom.current().nextBoolean() is used to create random numbers of type boolean

import java.util.concurrent.ThreadLocalRandom; class RandomNumberGeneration { public static void main( String args[] ) { // Generation of random integer values int rand_int = ThreadLocalRandom.current().nextInt(); // Display random integers System.out.println("Random integer values: " + rand_int); // Generation of double Random values double rand_double = ThreadLocalRandom.current().nextDouble(); // Display random doubles System.out.println("Random Double Values: " + rand_double); //Generating Random Boolean Values boolean rand_boolean = ThreadLocalRandom.current().nextBoolean(); // Display random booleans System.out.println("Random Boolean Values: " + rand_boolean); } }

### Java 8’s Random Number Generator

The Random class now contains a new method int(), introduced in Java 8. Before using the method, we must first import the java.util.Random package.

**ints():**

The pseudorandom int values are generated in the same way as the nextInt() method is called. It generates an infinite number of pseudorandom integer values.

long streamSize: ints(long streamSize): ints(long streamSize): int

The method parses a long-type parameter streamSize. It defines how many values should be generated. The pseudorandom int values are generated in the same way as the nextInt() method is called. It also returns a stream of int values that are created at random. On the off chance that the stream’s size is less than zero, it throws an IllegalArgumentException.

ints(long streamSize, int randomNumberOrigin, int randomNumberBound):

Parameters:

- streamSize: Number of values to generate.
- NumberOrigin: Each random value’s origin.
- NumberBound: Each random number has abounded.

It generates a stream of pseudorandom int values with the origin and bound given. If any of the following conditions are met, it throws an IllegalArgumentException:

bound stramSize 0 origin > stramSize 0 origin > stramSize 0 origin > stramSize

**Parameters**:

- randomNumberOrigin: Each random value’s origin.
- randomNumberBound: Each random value’s bound

It generates an infinite stream of pseudorandom int values with the origin and bound supplied. If the origin is equal to bound or greater, it produces an IllegalArgumentException. Similarly, the longs() and doubles() methods generate a stream of long and double types, respectively. Let’s write a program that uses the Random class’s ints() method to generate a stream of numbers.

import java.util.Random; public class RandomNumberGeneration { public static void main(String[] args) { randomInts(4); randomInts(29, 70, 110); //getStreamOfRandomInts(30, 120); } //method generating integer streams of size 4 public static void findRandomInts(int int_val) { Random rand_val = new Random(); rand_val.ints(int_val).forEach(System.out::println); } //method generating 9 integer streams between 50 to 120 public static void findRandomInts(int int_val, int int_origin, int int_bound) { Random rand_val = new Random(); rand_val.ints(int_val, int_origin, int_bound).forEach(System.out::println); } }

### Using the Math.random function

Follow the steps below to generate random integers inside a range using Math.random():

- Declare the range’s minimum value.
- Declare the range’s maximum value.

To produce numbers with the min and max values included, use the formula Math.floor(Math.random()*(max-min+1)+min). In fact, you can only use this method if you need a random integer or float value.

class RandomNumberGeneration { public static void main( String args[] ) { int min_val = 250; int max_val = 400; //Generation of random integer values from 250 to 400 System.out.println("Random integer values from "+min_val+" to "+max_val+ ":"); int rand_integer = (int)Math.floor(Math.random()*(max_val-min_val+1)+min_val); System.out.println(rand_integer); } }

## Conclusion

We often need to create random numbers while developing applications in Java programming. Many programs can generate random numbers, such as the OTP, which validates the user. Dice implementation is one of the best illustrations of random numbers. A random number is shown between 1 and 6 when we throw it.