Troubles with rand function

I'm having a bit of trouble with randomizing numbers for my recreation of the Monty Hall Problem. Here's my code. (Posting all of it, because the problem could be anywhere and I'm just overlooking 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

int rnd()
{
    srand ( time(NULL) );
    return rand() % 2;
}

int main()
{
    int d1 = 0;
    int d2 = 0;
    int d3 = 0;
    int wins = 0;
    int runs = 0;
    int c1;
    int percentage;



    do
    {
        while (d1 + d2 + d3 == 0)
        {
            d1 = rnd();
            if (d1 = 1)
            {
                d2 = 0;
                d3 = 0;
                break;
            }
            else
            {
                d2 = rnd();
                if (d2 = 1)
                {
                    d1 = 0;
                    d3 = 0;
                    break;
                }
                else
                {
                    d3 = rnd();
                    if (d3 = 1)
                    {
                        d1 = 0;
                        d2 = 0;
                        break;
                    }
                }
            }
        }

        c1 = d1;
        if (d2 = 0)
        {
            c1 = d3;
            runs = runs + 1;
            if (c1 = 1)
            {
                wins = wins + 1;
            }
        }
        else
        {
            c1 = d2;
            runs = runs + 1;
            if (c1 = 1)
            {
                wins = wins + 1;
            }

        }
        cout << runs << ". " << d1 << ", " << d2 << ", " << d3 << endl;
    } while (runs != 10);



    percentage = (wins / runs) * 100;

    cout << "After " << runs << " runs, switching gives " << percentage << " percent success rate" <<endl;
    system("PAUSE");
    return 0;
}


When I run the program, d1 is always equal to 1 no matter what. Whether I have the program set to 1000 iterations or 10, it's always equal to one.
Don't call srand for every random number. You should only call it once at the start of your program.
Last edited on
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
srand ( time(NULL) );

    do
    {
        while (d1 + d2 + d3 == 0)
        {
            d1 = rand() % 2;
            if (d1 = 1)
            {
                d2 = 0;
                d3 = 0;
                break;
            }
            else
            {
                d2 = rand() % 2;
                if (d2 = 1)
                {
                    d1 = 0;
                    d3 = 0;
                    break;
                }
                else
                {
                    d3 = rand() % 2;
                    if (d3 = 1)
                    {
                        d1 = 0;
                        d2 = 0;
                        break;
                    }
                }
            }
        }


Calling srand only one time produces the same output.
Last edited on
if (d1 = 1)

= is assignment
== is comparison

you're assigning d1 to 1 there, not comparing it to see whether or not it's 1.

You're doing that all over the place in your program.
Wow... You're completely right. I'm a little out of practice :P Thanks!
Topic archived. No new replies allowed.