Loading…

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

Example

                                        

This is done Using java.util.Random class:
import java.util.Random;
Random rand = new Random();
int n = rand.nextInt(100) + 1;
//100 is the maximum and the 1 is our minimum.

Suggest An Answer

No suggestions avaliable!

Latest post from Java interview questions

Ask Question