# Small Number guessing game

Page 1 of 1

## 2 Replies - 8057 Views - Last Post: 13 October 2009 - 01:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=131772&amp;s=8f7a3651ef4eb5fe3db9aff856c03063&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cavo

Reputation: 2
• Posts: 12
• Joined: 29-September 09

# Small Number guessing game

Posted 13 October 2009 - 08:43 AM

Hi Guys. i am trying to write a small guessing game program. The program asks a user for a range of values (max and min). The program generates a random number within this range and the player has to guess this number. A counter is also kept. I am having difficulty creating a random number between the two values. Please see my code below. Any suggestions? Thanks

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(int argc, char *argv[]) {
int randomNumber = 0;
int guess = 0;
int maxnumber;
int minnumber;

cout << "Please Enter a Max Number \n";
cin >> maxnumber;

cout << "Please Enter a Min Number \n";
cin >> minnumber;

// Create random number between min and max values
srand(time(NULL));
randomNumber = (rand() % (maxnumber - minnumber + 1)) + maxnumber;

// Loops forever until user finds the number
while (guess != randomNumber)
{
cout<<"Pick a number : ";
cin >> guess;

if (randomNumber > guess)
cout << "Higher!"<<endl<<endl;

else if (randomNumber < guess)
cout << "Lower!"<<endl<<endl;

else
cout << "Good job! You found the number!"<<endl<<endl;

}

return 0;
}

Is This A Good Question/Topic? 0

## Replies To: Small Number guessing game

### #2 Shamayla

Reputation: 3
• Posts: 12
• Joined: 09-October 09

## Re: Small Number guessing game

Posted 13 October 2009 - 09:23 AM

do this

int div = maxNumber-minNumber+1;
randomNumber = rand()%div;
randomNumber += minNumber;

How it works

lets suppose minNumber= 20;
maxnumber = 30;
now div = 11;

when i do rand()%11 a number between 0-10 is stored in randomNumber.
When i add it to minNumber randomNumber will be b/w 20-30.
Hope this works for u!
Do thank me if u find the post useful!

This post has been edited by Shamayla: 13 October 2009 - 09:27 AM

### #3 cavo

Reputation: 2
• Posts: 12
• Joined: 29-September 09

## Re: Small Number guessing game

Posted 13 October 2009 - 01:31 PM

Shamayla, on 13 Oct, 2009 - 08:23 AM, said:

do this

int div = maxNumber-minNumber+1;
randomNumber = rand()%div;
randomNumber += minNumber;

How it works

lets suppose minNumber= 20;
maxnumber = 30;
now div = 11;

when i do rand()%11 a number between 0-10 is stored in randomNumber.
When i add it to minNumber randomNumber will be b/w 20-30.
Hope this works for u!
Do thank me if u find the post useful!

Hey thanks for the reply. The code works but i don't understand it! Why are we assigning int div teh value of the max number minus the min number with one added to it?Also why is a number stored between 0-10 when u do rand()%11? Sorry im a newbie!