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

>int result = (int) (rand.nextInt());
Should be:

int result = (int) (rand.nextInt(num));

By the way, Sun has a complete API listing for Java that makes a wonderful reference when first learning: http://java.sun.com/j2se/1.4.2/docs/api/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.