HI I was wondering if you can help me with this random number program.

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;

class GuessAgain {

    public static void main(String args[]) {
        Scanner myScanner = new Scanner(System.in);

        int numGuesses = 0;
        int randomNumber = new Random().nextInt(10) + 1;

        out.println("       ************         ");
        out.println("Welcome to the Guessing Game");
        out.println("       ************         ");

        out.print("Enter an int from 1 to 10: ");
        int inputNumber = myScanner.nextInt();

        while (inputNumber != randomNumber) {
            out.println("Try again...");
            out.print("Enter an int from 1 to 10: ");
            inputNumber = myScanner.nextInt();

        out.print("You win after ");
        out.println(numGuesses + " guesses.");

The problem I am having is with this line int randomNumber = new Random().nextInt(10) + 1;
Why do we need to add a nextInt() method to it, I don't understand? Was this int randomNumber = new Random(10 + 1); enough?

Recommended Answers

All 9 Replies

The method nextInt(A_NUMBER) of Random class will return a number between 0 up to A_NUMBER inclusive. It could be written in mathematic notation as [0, A_NUMBER). If you are going to pick a random value from 1 to 10, you simply need a random number of 0~9 and add 1.

int randomNumber = new Random().nextInt(10);  // return value of 0 ~ 9
int randomNumber = new Random().nextInt(10) + 1;  // return value of 1 ~ 10
int randomNumber = new Random().nextInt(10 + 1);  // return value of 0 ~ 10

Do you want to include number 0 as well or do you want it to start from 1?

Oh that's ok thanks, I think I have understood the way it works, roughly, what was bugging me was why do we have to use nextInt(), not quite sure what its role is

Please read Integer API for more detail about the method.

No, you do not need to use nextInt() but it is more convenient that way. If you want to generate a random number (no decimal), you may do it from nextFloat(), multiply it, and floor the multiply result. You would still get the same thing as nextInt(). In Java, they have already done the work for you, so there is no reason to reinvent the wheel. That's all.

The Random class is a random number generator. It can generate all kinds of random values, eg ints, gaussian distributed doubles, arrays of random bytes etc.
new Random() gives you a new random number generator - not a randon number, but a random nunber generator. You then use that to generate random numbers, eg by calling nextInt for a random integer, nextBytes for an array of random byte values etc.

ah I see, thanks guys, so effectively this on its own int randomNumber = new Random() is useless, I have to tell it what kind of number I want to generate.
I guess what confused me was the fact that nextInt() was used in things like user input to get the next input, and I couldn't understand why in hereint randomNumber = new Random().nextInt(10) + 1;we had to get the next int. So to summarize in the random number case, nextInt() has a different "meaning" so to speak

Yes, int randomNumber = new Random(); is worse than useless, it won't even compile. A random number generator is not the same as an integer, so you can't just assign one to the other.

You can also use the same generator repeatedly for multiple random numbers, eg

Random generator = new Random();
for (int i = 0; i<10; i++) {
    // generate 10 random ints
    Sytem .out .println(generator.nextInt());

You will find many many examples in Java where two classes have methods with the same name. In general they will do something similar (that's why they have similar names), but the details will be completely different. Java is smart enough to see what kind of object you are using when you invoke the method, so it knows which version to use.

Thanks, I guess it will come with experience, I am still very new to java. Is this difference detailed in the API, I coldn't find it...

Yup, it comes with experience.
You don't need to worry about methods having the same name. The API doc tells you everything about the methods for any given class, and that's all you need.
The exact details of how Java finds the right method is documented in the Language Specification, not the APi doc, but honestly I wouldn't worry about that just yet.

Fab, thanks a lot for your help

Be a part of the DaniWeb community

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