0

Hi,

Can anyone tell me how to tweak my code so it will only generate a random number between 0 and the number input by user?

Here's the code:

import java.util.Random;
import java.io.*;

public class RandomNumPicker
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader stdin = new BufferedReader
        (new InputStreamReader(System.in));
        
       int num;
       
       Random rand = new Random();
       
       System.out.print("Enter a number to choose a random number from: ");
       num = Integer.parseInt(stdin.readLine());
       
       int result = (int) (rand.nextInt());
       
       System.out.print("The random number is: " + result);
    }
}

Here's a sample run:

Enter a number to choose a random number from: 100
The random number is: 664075476

Thank you

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by Narue
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.