Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Mathematical Library Methods

Chapter 7

Mathematical Library Methods

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Generate Random Integers in Given Ranges


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

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

import java.util.Scanner;

 

public class RandomNumberRange {

    public static void main(String[] args) {

 

        // Random number between 10 and 20 (inclusive)

        int r1 = (int)(Math.random() * (20 - 10 + 1)) + 10;

 

        // Random number between 25 and 50 (inclusive)

        int r2 = (int)(Math.random() * (50 - 25 + 1)) + 25;

 

        // Output

        System.out.println("Random number between 10 and 20: " + r1);

        System.out.println("Random number between 25 and 50: " + r2);

    }

}

Output


Sample Output 
Random number between 10 and 20: 17
Random number between 25 and 50: 43

Explanation

Formula Used

Random Integer = (int) (Math.random() × (max - min + 1)) + min
  • Math.random() generates a value between 0.0 and 0.999...
  • Multiplying by (max - min + 1) sets the range size
  • Adding min shifts the range to the required starting value
  • Type casting to int removes the decimal part