# Need a little help with this random numers generator.

• 05-05-2013, 06:14 PM
MR bruto
Need a little help with this random numers generator.
alright so i decided to improve my randoms numbers guessing game.

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:
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);                                                                         } }```
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:
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);                                                                         } }```
• 05-05-2013, 08:05 PM
JosAH
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:

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
• 05-06-2013, 03:43 AM
Junky
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.