# Thread: Help with Random Number Generator

1. Member
Join Date
Mar 2011
Posts
1
Rep Power
0

## Help with Random Number Generator

Hey guys

I need to make a program that is a guessing game. The user enters a number from 1 to 20 and my program tries to guess it using its previous guesses as parameters in a random number generator. The problem I'm having is how to generate a random number from the first guess to the number 20 (look at my firstGuessLow method) and how to generate a random number from the lowest possible guess to the highest possible guess (look at my secondGuess method).

Java Code:
```import java.util.Scanner;
import java.util.Random;

public class GuessingGame {

Random numberGenerator = new Random();
int number, userNumber, highNum, lowNum, minMaxNum;
String input;

public GuessingGame(Scanner input)
{
number = numberGenerator.nextInt();

}
public void StartGame()
{
/* Print an invitation to the user to think
*  of a number from 1 to 20
*/
System.out.println("Type a number from 1 to 20");

// Give the user the first guess and store it in number
System.out.println("I will try to guess your number.");
number = numberGenerator.nextInt(20) + 1;

// Consume the remaining newline

System.out.println("My guess is " + number + ". Is this number high, low, or correct?");

if (input.equals("high")){
firstGuessHigh(number);
}
else if (input.equals("low")){
firstGuessLow(number);
}
else if (input.equals("correct"))
{
System.out.println("I win!");
}
}
public void firstGuessHigh (int highestValue)
{
/*
* Generate a random number from 1 to highestValue
*/
highNum = numberGenerator.nextInt(highestValue) - 1;

System.out.println("My second guess is " + highNum + ". Is this number high, low, or correct?");
if (input.equals("high")){
secondGuess(1, highNum);
}
else if (input.equals("low")){
secondGuess(highNum, number);
}
else if (input.equals("correct"))
{
System.out.println("I win! It only took me two tries!");
}

}
public void firstGuessLow (int lowestPossible)
{
/*
* Generate a random number from lowestPossible to 20
*/

lowNum = numberGenerator.nextInt(lowestPossible) + lowestPossible + 1;
System.out.println("My second guess is " + lowNum + ". Is this number high, low, or correct?");
if (input.equals("high")){
secondGuess(number, lowNum);
}
else if (input.equals("low")){
secondGuess(lowNum, 20);
}
else if (input.equals("correct"))
{
System.out.println("I win! It only took me two tries!");
}
}
public void secondGuess (int minGuess, int maxGuess)
{
// Generate a random number from minGuess to maxGuess
minMaxNum = numberGenerator.nextInt(maxGuess) + minGuess;

System.out.println("My final guess is " + minMaxNum + ". Is this number correct?(Please type yes or no)");
if (input.equals("yes")){
System.out.println("I win! It only took me three tries!");
}
else if (input.equals("no")){
System.out.println("You win! Congratulations!");
}
}
}```
Last edited by celtics; 03-07-2011 at 09:25 PM.

#### Posting Permissions

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