In this post, we will see how to use the pseudo-random number generator in Java. Before that, we will see what is Random numbers.
Random numbers are the numbers that occur in sequence such that the values are uniformly distributed over a defined interval or set, which is impossible to predict future values based on present and past value. Also it should pass a bunch of statistical tests. Random Number are important in statistical analysis and probability theory.
The algorithm used for generating the random number is called Random Number Generator (RNG)
There are two types of random numbers –
- Pseudo-Random Number (PRN)
- Cryptographically Secure Pseudo-Random Number (CSPRN)
1. Pseudo-Random Number Generator in Java (PRN)
A pseudo-random Number Generator in Java (PRNG) is an implementation of an algorithm that produces the number in the sequence that is uniformly distributed numbers that looks random, but is, in fact, deterministic (the sequence is generated from some unknown internal state), hence pseudo-random.
When you use Pseudo-random numbers in statistics, the only thing you care about is the sequence of numbers you get from the generator that passes the bunch of statistical tests and whether it’s distributed like random numbers.
There are 3 ways to generate the pseudo-random Number in Java as below –
- Math.random()
- util.Random.nextInt()
- util.Random.ints() [ Java 8]
1.1 Random Number Generator with Math.random()
In java Math.random() method by default generates the random double number between 0.0 to 1.0 by calling the method as shown in below java code.
/**
* Created on Nov 19, 2016 Copyright(c) https://kodehelp.com All Rights Reserved.
*/
package com.kodehelp.util;
/**
* @author https://kodehelp.com
*/
public class PseudoRandomNumber {
public static void main(String args[]) {
System.out.println(Math.random());
}
}
If you want integer numbers in a specified range using Math.random() method then you can use the approach shown in the below java code.
/**
* Created on Nov 19, 2016 Copyright(c) https://kodehelp.com All Rights Reserved.
*/
package com.kodehelp.java.util;
/**
* @author https://kodehelp.com
*/
public class PseudoRandomNumber1 {
public static void main(String args[]) {
int MIN = 1001;
int MAX = 100001;
for (int i = 0; i < 10; i++) {
System.out.println((int) (Math.random() * ((MAX - MIN) + 1)) + MIN);
}
}
}
Note: Math.random() uses Random.nextDouble() internally and Random.nextDouble() uses Random.next() twice to generate a double number that has approximately uniformly distributed bits in its mantissa, so it is uniformly distributed in the range 0 to 1-(2^-52).
1.2 Random Number Generator with java.util.Random
java.util.Random
class used to generate a stream of pseudorandom numbers. As per the Oracle Java documentation, this class uses a 48-bit seed, which is modified using a linear congruential formula. Instances of java.util.Random is threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.
java.util.Random().nextInt()
generates a pseudorandom integer number uniformly distributed int value from this random number generator’s sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 2³² possible int values are produced with (approximately) equal probability.
java.util.Random().nextInt(int bound)
generates a pseudorandom integer number from 0 (inclusive) to bound (exclusive).
See below java code for Random().nextInt()
and Random.nextInt(int bound)
/**
* Created on Nov 19, 2016 Copyright(c) https://kodehelp.com All Rights Reserved.
*/
package com.kodehelp.java.util;
import java.util.Random;
/**
* @author https://kodehelp.com
*/
public class PseudoRandomNumber {
public static void main(String args[]) {
int bound = 100001;
Random r = new Random();
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt());
}
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt(bound));
}
}
}
1.3 Random Number Generator with java.util.Random().ints()
In Java 8, below new methods are added in java.util.Random class.
public IntStream ints(int randomNumberOrigin, int randomNumberBound)
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
This Random().ints(int origin, int bound)
or Random().ints(int min, int max)
generates a random integer from origin (inclusive) to bound (exclusive).
See below java code for Random().ints()
example to generate random numbers in a given range –
/**
* Created on Nov 19, 2016 Copyright(c) https://kodehelp.com All Rights Reserved.
*/
package com.kodehelp.java.util;
import java.util.Random;
/**
* @author https://kodehelp.com
*/
public class GenerateRandomNumber {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
System.out.println(getRandomNumberInRange(111, 200));
}
}
private static int getRandomNumberInRange(int randomNumberOrigin, int randomNumberBound) {
Random r = new Random();
return r.ints(randomNumberOrigin, (randomNumberBound + 1)).limit(1).findFirst().getAsInt();
}
}
Please see my post about JavaScript Random Number Generator.