How to Generate a Random Number in Java


In this example we will show how to generate a random number using random method in Java.

Source Code

package com.beginner.examples;

public class RandomExample {

	public static void main(String[] args) {

		//generate a random number
		int rand = (int)(Math.random()* 10 ); 
		System.out.println(rand);
    }
}

Output:

9
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments