ICSE Class 10 Computer Applications Question 11 of 14

Mathematical Library Methods — Question 11

Back to all questions
11
Question

Question 11

Write a program to generate random integers in the following ranges:

i. 10 to 20 (both inclusive)
ii. 25 to 50 (both inclusive)

Answer
public class KboatRandom
{
    public static void main(String args[]) {
        int min = 10, max = 20;
        int range = max - min + 1;
        int num = (int)(range * Math.random() + min);
        System.out.println("Random number in the range 10 to 20: " + num);
        
        min = 25;
        max = 50;
        range = max - min + 1;
        num = (int)(range * Math.random() + min);
        System.out.println("Random number in the range 25 to 50: " + num);
    }
}
Output
BlueJ output of KboatRandom.java