Java program to have random number(s) within a specified limit

Code

package com.physicsinfo;

import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        System.out.print("Enter the highest integer within which you want the random number: ");
        Scanner sc = new Scanner(System.in);
        int highest = sc.nextInt();
        System.out.print("How many random numbers do you want to have printed: ");
        int count = sc.nextInt();
        double rand;
        System.out.println("Here is/are your random number(s): ");
        for(int i=1;i<=count;i++) {
            rand = highest * Math.random();
            System.out.print((int)rand+" ");
            if(i%10==0){
                System.out.println(" ");
            }
        }
    }
}

Output

Java random number program output

Leave a Comment

Your email address will not be published.