random number code just doesn't work

I am getting some weird results from this code. Well, weird for me anyway.
It is just a simple program, where I want to generate a random number, then use that random number to output a string. I have inserted multiple cout statements trying to determine what is wrong. I expected the first two output numbers to be the same, but they are not. They are always different. The third number output, I expected to change randomly. It always comes p a "1". Obviously I am a beginner and right now I am just stuck. Thanks for reading.

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
#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int randRange (int low, int high)

{
    return rand () % (high - low + 1) + low;
}
    int num = rand() % 10;

int main ()
{
    srand ( time ( NULL ));
    for ( int i = 0; i < 1; ++i)
    {
        int num = rand() % 10;
        cout << rand () % 10 << endl;
        cout << num << endl;
    }

    if (num = 0)
    {
        cout << num << endl;
        cout << "this is number zero" << endl;
    }
    else if (num = 1)
    {
        cout << num << endl;
        cout << "This is number one" << endl;
    }
    else if (num = 2)
    {
        cout << num << endl;
        cout << "This is number two" << endl;
    }

    return 0;
}
I expected the first two output numbers to be the same, but they are not.

If you are expecting line 20 and 21 to be the same, they won't be. You're making two calls to rand(). Each call will return a different random number.

Lines 24,29,34: You're using the assignment operator (=), not the equality operator (==). As written, line 24 will always be false and line 29 will always be true.

randRange() isn't used, is that an oversight?

AFAICS, the for loop will run once, assigning a random value to num, and then printing another random value to the screen, before finally printing num.

Were these the 2 numbers you expected to be the same? If so, your expectation is wrong, expect them to be different.

In line 12, you assign a value to global variable num. Since you haven't seeded the random number generator yet, this value will be the same every time you run your program.
In line 19, you overshadow that variable num, by reusing it at a local scope. Note that num in line 19 is not the same as num from line 12. By line 23, num from line 19 has ceased to exist, and you're left with num from line 12, and it's not so random value. This is why you get the same thing every time for your 3rd output. If the value assigned at line 12 was anything other than 0, 1 or 2, you'd have no 3rd output whatsoever.

EDIT:
I hadn't noticed the use of "=" instead of "==". That's the real reason you always get 1.
Last edited on
Okay, and thanks so much for getting me through this. It is difficult to get up and running with coding and you people are giants among the newby dwarfs. :)
Topic archived. No new replies allowed.