The java.util.Random class instance is used to generate a stream of pseudorandom numbers. Following are the important points about Random −
java.util.Random
Syntax:
To generate a random integer in the range 0 to 100 both inclusive.
// create instance of Random class Random rand = new Random(); int rand_int1 = rand.nextInt(101);
Task:
What will be the output for the following code?
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class myClass { public static void main (String[] args) throws java.lang.Exception { // your code goes here Random rand = new Random(); System.out.println(rand.nextInt(1)); } }