Receiving random numbers below zero, when range set by user

Jul 19, 2012 at 8:17pm
Hello, I've recently started programming again after a long intermission of a year or so (Only ever learned basic programming in Visual Basic) But I've decided to start with the ever famous number guessing game to get a base understanding of C++. Right now the program is all in one function (main()) and eventually will go into separate functions and header files. Everything was going fine until I added 'Set your own range of values for random int generation', which is where I have an issue. When the user sets the values, the generator creates a negative number for some reason. Here's the source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

unsigned int GetRandomNumber(int nMax, int nMin);
int main ()
{
    using namespace std;
    srand(static_cast<int>(clock()));
    bool UserCorrect = false, UserPlay = true;
    while (UserPlay) {
        cout << "Welcome to the Number Guessing Game!" << endl;
        cout << "Would you like to set the limits? (Y / N)" << endl;
        char chSetLimit;
        cin >> chSetLimit;
        int nRndNumber;
        if (chSetLimit == 'Y' || chSetLimit == 'y') {
            //User imputs range of possible random int
            int nMinSet, nMaxSet;
            cout << "Set Minimum: " << endl;
            cin >> nMinSet;
            cout << "Set Maximum: " << endl;
            cin >> nMaxSet;
            cout << "Generating a random number between: " << nMinSet << " and " << nMaxSet  << endl;
            nRndNumber = GetRandomNumber(nMaxSet, nMinSet);
            
        }
        else {
            //If User does not want to imput range of possible random int
            int nMaxDefault = 100;
            int nMinDefault = 0;
            cout << "Generating a random number between: " << nMinDefault << " and " << nMaxDefault  << endl;
            nRndNumber = GetRandomNumber(nMaxDefault, nMinDefault);
        }
        
        //For testing purposes
        cout << nRndNumber << endl;;
        
        int nUserGuess = 0;
        int nUserGuessedTimes = 0;
        while (!UserCorrect) {
            cout << "Please enter a valid guess." << endl;
            cin >> nUserGuess;
            nUserGuessedTimes++;
            if (nUserGuess > nRndNumber) {
                cout << "Your current guess is too high." << endl;
            }
            else if (nUserGuess < nRndNumber) {
                cout << "Your current guess is too low." << endl;
            }
            else {
                cout << "You guessed correctly!" << endl;
                cout << "It took you " << nUserGuessedTimes << " guesses." << endl;
                UserCorrect = true;
            }
        }
        cout << "Do you want to continue?\n Press 'N' or 'n' if you would like to terminate program." << endl;
        char chContinue;
        cin >> chContinue;
        if ( chContinue == 'N' || chContinue == 'n')
            UserPlay = false; //Exit Prigram
        else UserCorrect = false; //Reset values
    }
    return 0;
}

unsigned int GetRandomNumber(int nMax, int nMin)
{
    return rand() % (nMax - nMin + 1) - nMin;
}




Here's a snippet from the output window:

Welcome to the Number Guessing Game!
Would you like to set the limits? (Y / N)
Y
Set Minimum:
10
Set Maximum:
20
Generating a random number between: 10 and 20
-8
Please enter a valid guess.


So this is where my problem is, if they choose not to pick the range, the numbers default and it works fine. By the way I'm running Xcode on 10.7.4 if that makes a difference. I'm usually on my desktop PC with Visual Studio Express but I'm not at home at the moment. But any help would be great, and Thank You for your time! Oh and sorry if you see some really bad ways to utilize C++, this is my first time programming in a 'real' language.
Last edited on Jul 19, 2012 at 8:30pm
Jul 20, 2012 at 1:25am
Quick look:

return rand() % (nMax - nMin + 1) - nMin;
The order of operations will take the random number THEN % by (nMax - nMin + 1) and THEN subtract nMin

say rand() == 2

THEN
2%(20-10) == 2%10 == 2

THEN
2-10 == -8
Jul 20, 2012 at 2:49am
That's where the problem is for sure. move nMin to the front and add everything to it.

return nMin + rand() % (nMax - nMin + 1);
Jul 20, 2012 at 3:06am
or another bracket to include that last neg nMin :D
Jul 20, 2012 at 12:00pm
Oh thank you. Completely overlooked that, so simple. Sorry for wasting your time :/ but thanks! )
Topic archived. No new replies allowed.