Random Number Generator Game

Started by
2 comments, last by Matt-D 11 years, 2 months ago

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));

Advertisement
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;

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 !!

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/

This topic is closed to new replies.

Advertisement