How we can generate random numbers in Java?
In Java, there are three ways to generate random numbers:
- java.util.Random class
- Math.random method
- ThreadLocalRandom class
BY Best Interview Question ON 03 Apr 2020
Example
This is done Using java.util.Random
class:
import java.util.Random;
//100 is the maximum and the 1 is our minimum.
Random rand = new Random();
int n = rand.nextInt(100) + 1;