Random Number Generator Not Working

I am trying to make a number guessing game and I have ALMOST everything working. The number generator, however, does not work properly. It only generates the number 1, so some help would be nice.

Here is the function and whatnot:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <ctime>
#ifndef Number_Guessing_Game_main_h
#define Number_Guessing_Game_main_h

int random(int low, int high)
{
if (int a = 0)
{
    srand((unsigned)time(0));
    a = 1;
}
    int rand_num;
    int range = (high - low) + 1;
    
    for (int i = 0; i<1; i++)
    {
        rand_num = low+int(range*rand()/(RAND_MAX + 1.0));
        return rand_num;
    }
}

#endif 


And here is just the main code in case you need it:

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
#include <iostream>
#include "main.h"

int main(void)
{
    using std::cout;
    using std::cin;
    
    int guess, num = random(1, 25);
    char response;
    do {
    cout << "\nWelcome to Number Guessing Game\n\n";
    cout << "Guess a number between 1 and 25: ";
    cin >> guess;

    if (guess == num)
    {
        cout << "\n\nYou have guessed correctly! The number was: " << num;
    }
    
    else {
        cout << "\n\nSorry. That guess was incorrect. The number was: " << num;
    }
    
        //Protection loop - begin
        do {
    cout << "\nDo you want to play again?(y/n) ";
    cin >> response;
        } while (response != 'y' && response != 'n');
        //Protection loop - end
        
    } while (response == 'y');
    return 0;
}
The if statement in random doesn't make sense. Call srand once, at the beginning of main instead.
That didn't work.
The problem is this line
rand_num = low+int(range*rand()/(RAND_MAX + 1.0));
The multiplication is done before the division, so there is a big chance that range*rand() will cause overflow.
How do I go about fixing this? I changed it to this:
rand_num = low+int(range*(rand()/(RAND_MAX + 1.0)));
That just made it always come out to 10.
Last edited on
Are you calling srand?
I call srand in the if statement. Then, you said it didn't make sense. So I moved it to the top of main and #included the libraries.
Last edited on
Then it should work.
Try replacing:

rand_num = low+int(range*rand()/(RAND_MAX + 1.0));

With:

rand_num = ( rand() % (range) ) + low;

The "rand () % (range)" portion will generate a pseudo-random number, then get the remainder of said number divided by the range. This effectively gets a number within the range you want, but it is between zero and said range, so you need to add low to ensure that the number will always be between low and high.
Last edited on
Also:
int guess, num = random(1, 25);

This is being called outside of the loop, so even if it is properly generated at the start of the program, it will be the same number every time the user wants to guess. It's okay to declare the variables here, but call
num = random(1, 25);
inside of your loop.
Last edited on
Topic archived. No new replies allowed.