# Generation of Random Integers in Java

Java provides us with ample options in order to generate any sort of random number that we need. This is mainly done in two ways:

• Using the Math.random() function
• Using the java.util.Random class

## Math.random() function

The Math.random() function returns pseudorandomly chosen numbers between 0.0 and 1.0, i.e. it returns a positive double value. When the expression is encountered, it generates a new pseudorandom-number generator which is used for the calls to this function. This function is synchronized and can be used by more than one thread. However, the contention might decrease for each thread to have its own pseudorandom-number generator if the pseudorandom numbers are to be generated at a great speed. This method does not throw any exceptions.

Package: java.lang. This is a default package in Java and the programmer doesn’t have to import it to access the Math.random() function.

### Using the Math.random() function to generate a random number

```/**
* Generating Random integers using the Math.random() function.
* It is inside the package java.lang
*/
package com.codingeek.java.RandomNumbers;

public class MathRandom {

public static void main(String args[]) {

//generating a random double integer. The range is 0 to 1.
double x = Math.random();

//generating a random integer from 1 to 100. Replace 100 by 101 to generate random from 0 to 100.
int y = (int) Math.floor(Math.random() * 100);

double sum = x + y;

System.out.println("Double no. :" + x);
System.out.println("Integer no. :" + y);
System.out.println("The sum is: " + sum);
}

}

```
```Output:-
Double no. :0.0025984574018734774
Integer no. :82
The sum is: 82.00259845740187
```

### Using the Math.random() function to generate random numbers in a given range

```/**
* Generating random integers using Math.random() in a given range.
*/
package com.codingeek.java.RandomNumbers;

import java.io.IOException;

public class MathRandomInARange {

/*
* this function returns the random number in the given range inclusive
*/
public static int generateRandom(int l,int r) {
int range = (r - l) + 1;
return (int)(Math.random() * range) + l;
}

public static void main(String args[])throws IOException {

//take the min and max range inclusive
int min = Integer.parseInt(in.readLine());
int max = Integer.parseInt(in.readLine());

System.out.println("The integer is:" + generateRandom(min,max));
}

}

```
```Output:-
12
20
The integer is:14
```

## Random class in Java

The Random class can be used to generate various types of random data, eg: boolean, float, int, long, double, etc. In order to generate random numbers, an instance of the Random class needs to be created.

The Random class uses a 48-bit seed, which is modified using the linear congruential formula. The algorithms use a protected utility method that can generate up to 32 pseudorandomly generated bits on each invocation. Instances of this class are thread-safe but are not cryptographically secure.

Package: java.util.

### Constructors of Random class

There are two constructors in the Random class. They are shown as follows:

• Random(): Used to create a new random number generator.
• Random(long seed): Used to create a new random number generator using a single long seed.

### Methods of Random Class

 Method Description public void nextBytes(byte[] bytes) Used to generate random bytes and place them into a user-provided byte array. public int nextInt()/public int nextInt(int bound) Returns the next pseudorandom, uniformly distributed int value from the random number generator’s sequence. If we specify the bound, the random number generated is between 0(inclusive) and bound(exclusive). public long nextLong() Returns the next pseudorandom, uniformly distributed long value from the random number generator’s sequence. public boolean nextBoolean() Returns the next pseudorandom, uniformly distributed boolean value from the random number generator’s sequence. public float nextFloat() Returns the next pseudorandom, uniformly distributed float value(between 0.0 and 1.0) from the random number generator’s sequence. public double nextDouble() Returns the next pseudorandom, uniformly distributed double value(between 0.0 and 1.0) from the random number generator’s sequence. public double nextGaussian() Returns the next pseudorandom, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from the random number generator’s sequence. public Intstream ints() Returns an effectively unlimited stream of pseudorandom int values. public Longstream longs() Returns an effectively unlimited stream of pseudorandom long values. public Doublestream doubles() Returns an effectively unlimited stream of pseudorandom double values. public void setSeed(long seed) used to set the seed of the random number generator using a single long seed.

### Using the Random class to generate a random number

```/**
*This program generates random numbers using the java.util.Random class.
*/
package com.codingeek.java.Random;

import java.util.Random;

public class RandomClass {
public static void main(String args[]) {

//create a Random class object
Random r = new Random();

//generating random integer in the range 0 to 10.
int x = r.nextInt(10);

//generating random double number between 0.0 to 1.0
double y = r.nextDouble();

double sum = x + y;

System.out.println("Double no. :" + y);
System.out.println("Integer no. :" + x);
System.out.println("The sum is: " + sum);

}

}

```
```Output:-
Double no. :0.6582175252636324
Integer no. :5
The sum is: 5.658217525263632
```

### Using the Random class to generate random numbers in a given range

```/**
* Generation of random integers in the given range using java.util.Random class
*/
package com.codingeek.java.RandomNumbers;

import java.io.IOException;
import java.util.Random;

public class RandomClassInARange {

public static void main(String args[])throws IOException {

//Random class
Random random = new Random();

int l = Integer.parseInt(in.readLine());
int r = Integer.parseInt(in.readLine());

//generate random integers
int range = (r - l) + 1;
int result = random.nextInt(range) + l;

System.out.println("The random integer is: " + result);
}
}

```
```Output:-
15
25
The random integer is: 17
```

So, that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Keep Coding!! Happy Coding!! 🙂