1.

How we can generate random numbers in Java?

Answer»

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.



Discussion

No Comment Found