Java Program to Generate Random Number

In this tutorial, we will learn how to write a Java program to generate a random number.

Java program to generate a random number

package com.javaguides.java.tutorial;

import java.util.Random;
import java.util.Scanner;

/**
 * Java Program to check if Number is Positive or Negative
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            Random rand = new Random();

            System.out.print("Please enter maximum range: ");
            int maxRange = scanner.nextInt();

            for (int loop = 1; loop <= 10; loop++) {
                System.out.println(rand.nextInt(maxRange));
            }
        }
    }
}
Output:
Please enter maximum range: 500
475
357
68
405
147
111
43
419
239
321

Related Java Programs


Comments