Generate random number

Generate random number

import java.util.*;
class GenerateRandomNumber {
   public static void main(String[] args) {
      int counter;
      Random rnum = new Random();
      /* Below code would generate 5 random numbers
       * between 0 and 200.
       */
      System.out.println("Random Numbers:");
      System.out.println("***************");
      for (counter = 1; counter <= 5; counter++) {
         System.out.println(rnum.nextInt(200));
      }
   }
}

Output 1

Random Numbers:
***************
135
173
5
17
15

The output of above program would not be same everytime. It would generate any 5 random numbers between 0 and 200 whenever you run this code. For e.g. When I ran it second time, it gave me the below output, which is entirely different from the above one.

Output 2

Random Numbers:
***************
46
99
191
7
134

 

Leave Comment

Important Topics

Title
Run and Compile
Hello World Program
User Input
Add Numbers
Sum of Two Numbers
Even Numbers
Odd numbers from 1 to n or 1 to 100
Even or Odd number
Average of 3 numbers
Fibonacci Series using loops
Generate random number
Largest of three Numbers
Decimal to octal conversion
Quotient and Remainder
Simple Interest
Compound Interest