• Create Account

## Random Number Generator Game

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Motoky  Members

183
Like
0Likes
Like

Posted 17 January 2013 - 10:00 AM

I am currently working through Beginning C++ Through Game Programming 3rd Edition, just to help brush up on my C++. I've only been through one C++ book once and it took me quite a while to understand it properly, but going through this so far is helping my quite a lot. I used to have trouble creating programs because I didn't know how to implement certain ideas into code.

I'm only on Chapter 2, I've got a random number game up and running. Basically, the book asks you to rewrite a program which asks the user to guess a random number by the compiler.

I set up a range for the random number which is between 1 and 100:

int minNumber = 1;

int maxNumber = 100;

int compsNumber = (rand() % maxNumber) + minNumber;

So it should create a random number between 1 and 100. The user enters a number and then a random number should be generated. It will enter a while loop and test the computers numbers to the users number:

if(compsNumber < myNumber)

{

...

minNumber = compsNumber + 1;

}

else if(compsNumber > myNumber)

{

...

maxNumber = compsNumber - 1;

}

So if the random number is less than the users number, it will tell the computer it is too low and set the minimum number (1) to the computers current number plus 1 (so any number below the previous guessed number and the guessed number doesn't show up again). If the computers number is too high, set the maximum number (100) to the computers current number minus 1 (so any number higher than the previous guessed number and the guessed number doesn't show up again).

It will keep looping through, displaying the guessed number, until the computers number matches the users number. At the end of the loop, I generate a new random number between the new range based on the new minimum and maximum ranges:

compsNumber = rand() % maxNumber + minNumber;

The problem I am having is that it's also generating a number above the maximum number which it shouldn't do. Depending on how big my number will determine how big the random number is, so it will sometimes go into the 300's. Am I generating a random number wrong or something??

I am seeding the random number generator like so:

srand(static_cast<unsigned int>(time(0));

### #2fastcall22  Moderators

9741
Like
4Likes
Like

Posted 17 January 2013 - 10:19 AM

The problem I am having is that it's also generating a number above the maximum number which it shouldn't do
int compsNumber = (rand() % maxNumber) + minNumber;

rand() % maxNumber returns a number between 0 and maxNumber-1... Adding minNumber gives you a number between minNumber and minNumber+maxNumber-1.

The following code will return a number between minNumber and maxNumber:
int compsNumber = rand()%(maxNumber-minNumber) + minNumber;


zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

### #3Poigahn  Members

594
Like
0Likes
Like

Posted 17 January 2013 - 01:26 PM

If you want a Number between 1 and the MaxNumber then You should only add 1 to the random roll, not the minNumber.  The random number generator generates a number based on the maxnumber but starts at 0 that is why you add 1 (+1) to the Random Number.

The Number you are adding through each loop should be to the number of guesses, not the compNumber.

Re-read that chapter and double check your code, I have the same book and the code works.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

### #4Matt-D  Members

1572
Like
0Likes
Like

Posted 17 January 2013 - 01:50 PM

Since you're using C++, there's an easier way to do it:
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

If you're curious, random_device is not necessary, but it does something way more fun than seeding with time:
- Windows/VS: http://stackoverflow.com/questions/9549357/the-implementation-of-random-device-in-vs2010
- Linux: http://en.cppreference.com/w/cpp/numeric/random/random_device/random_device
- Boost: http://www.boost.org/doc/html/boost/random/random_device.html
More:
http://en.cppreference.com/w/cpp/numeric/random/random_device
http://www.cplusplus.com/reference/random/random_device/

Edited by Matt-D, 17 January 2013 - 02:05 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.