# Thread: Need a little help with this random numers generator.

1. ## Need a little help with this random numers generator.

alright so i decided to improve my randoms numbers guessing game.

Java Code:
```import java.util.Scanner;
import java.util.Random;
public class excercise {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
Random rand = new Random();
int rand1 = rand.nextInt(10);
int numguesses = 1;
System.out.println("Guess the right number 1-10: ");
while (rand1 != input.nextInt()){
System.out.println("Wrong! try again!");
System.out.println("Guess the right number 1-10: ");
numguesses++;
}

System.out.println("Right! Turns used: "+numguesses);

}
}```
i really want to make it so that it tells you "Your number was not in the range of 1-10".
i did it by this:
Java Code:
```import java.util.Scanner;
import java.util.Random;
public class excercise {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
Random rand = new Random();
int rand1 = rand.nextInt(10);
int numguesses = 1;
while (input.nextInt() > 10){
System.out.println ("The number you entered is out of the range 1-10! try again!");
System.out.println("Guess the right number 1-10: ");
}

System.out.println("Guess the right number 1-10: ");
while (rand1 != input.nextInt()){
System.out.println("Wrong! try again!");
System.out.println("Guess the right number 1-10: ");
numguesses++;
}

System.out.println("Right! Turns used: "+numguesses);

}
}```
Java Code:
```	while (input.nextInt() > 10){
System.out.println ("The number you entered is out of the range 1-10! try again!");
System.out.println("Guess the right number 1-10: ");
}```
but it doesnt seem to work for me.

can anybody tell me how to fix it?
because when i run it, and you enter your first number it basically does NOTHING. then if you enter your second number it just starts from here:
Java Code:
```System.out.println("Guess the right number 1-10: ");
while (rand1 != input.nextInt()){
System.out.println("Wrong! try again!");
System.out.println("Guess the right number 1-10: ");
numguesses++;
}

System.out.println("Right! Turns used: "+numguesses);

}
}```

2. ## Re: Need a little help with this random numers generator.

Try to say in words what you want; don't go into too much detail, that's for later. Try to use terms such as 'if' and 'while' and 'do'. A nice start would be:

Java Code:
```do
get a valid number
vaidate the number
while the number is not correct```
the phrase 'get a valid number' begs to be a separate method; methods are a fine way to unravel deeply nested control flow. Next try to go into more detail for the first phrase: what is a valid number? What is a number?

kind regards,

Jos

3. ## Re: Need a little help with this random numers generator.

From the API for nextInt(n):

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Therefore your code generates numbers from 0 - 9. Not what you want.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•