Java Program to generate random numbers between 1 to 10


In this program, You will learn how to generate random numbers from 1 to 10 in java.


Generate Random number

((int) (Math.random() * (10 - 2)) + 2)

Example: How to generate random numbers between 1 to 10 in java.

class Main {

    public static void main(String args[]) {

        for (int i = 1; i <= 10; i++) {

            System.out.print("Random Number is :");
            System.out.println((int) (Math.random() * (10 - 2)) + 2);
        }

    }
}

Output:

Random Number is :4
Random Number is :9
Random Number is :6
Random Number is :5
Random Number is :8
Random Number is :8
Random Number is :7
Random Number is :9
Random Number is :3
Random Number is :9