rand() - simulation program - seed

I had a previous post about this same program, but now I am at a new topic, so I created a new thread. Below, I am using
1
2
cout << jumps << endl;
cout << loss << endl;

to view the resulting numbers, and disregarding the displayInfo right now. The problem is that every time the loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 while (simRun < 501)
    {
        while (runs != 1)
        {
            jumpFleas (grid1, grid2, loss);
            jumps += 1;
            runs = checkSquares(grid2);
            resetGrid(grid1, grid2);
            zeroOut(grid2);
        }
        cout << jumps << endl;
        cout << loss << endl;
        simRun++;
    }

runs through, I get the exact same number of jumps and loss. I need these to be different, because this is a simulation program. I've tried seeding to no avail. Looking for some thoughts on how to fix this.

The full program:
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int checkSquares(int [][22]);
void jumpFleas(int [][22], int [][22], int &);
void resetGrid(int [][22], int [][22]);
void zeroOut(int [][22]);
void initialize(int [][22]);
void getTotals(int, int, int, int);
void getMinMax(int, int, int);
void displayInfo(int, int, int, int);

int main()
{
    
    int simRun = 0;
    int min = 10000, max = 0, jumps = 0, loss = 0, runs = 0;
    int TOTALJ = 0, TOTALL = 0;
    int grid1[22][22];
    int grid2[22][22];
    initialize(grid1);
    zeroOut(grid2);
    
  
    
    while (simRun < 501)
    {
        while (runs != 1)
        {
            jumpFleas (grid1, grid2, loss);
            jumps += 1;
            runs = checkSquares(grid2);
            resetGrid(grid1, grid2);
            zeroOut(grid2);
        }
        cout << jumps << endl;
        cout << loss << endl;
        simRun++;
    }
    
    getTotals(jumps, loss, TOTALJ, TOTALL);
    getMinMax(jumps, min, max);

    displayInfo(TOTALJ, TOTALL, min, max);


    return 0;
}

void jumpFleas(int grid1[][22], int grid2[][22], int &loss)
{
    int r,c, move;
    
    for (r=1; r<22; r++)
    {
        for (c=1; c<22; c++)
        {
            
            move = (rand() % 100) + 1;
            
            if (move>0 && move<16)
            {
                grid2[r][c] += grid1[r][c];
            }
            if (move>15 && move<31)
            {
                grid2[r+1][c] += grid1 [r][c];
                if ((r+1) == 22)
                {
                    loss += 1;
                }
            }
            if (move>30 && move<36)
            {
                grid2[r+1][c+1] += grid1[r][c];
                if (((r+1) == 22) || ((c+1) == 22))
                {
                    loss += 1;
                }
            }
            if (move>35 && move<56)
            {
                grid2[r][c+1] += grid1 [r][c];
                if ((c+1) == 22)
                {
                    loss += 1;
                }
            }
            if (move>55 && move<61)
            {
                grid2[r-1][c+1] += grid1 [r][c];
                if (((r-1) == 0) || ((c+1) == 22))
                {
                    loss += 1;
                }
            }
            if (move>60 && move<76)
            {
                grid2[r-1][c] += grid1[r][c];
                if ((r-1) == 0)
                {
                    loss += 1;
                }
            }
            if (move>75 && move<81)
            {
                grid2[r-1][c-1] += grid1[r][c];
                if (((r-1) == 0) || ((c-1) == 0))
                {
                    loss += 1;
                }
            }
            if (move>80 && move<96)
            {
                grid2[r][c-1] += grid1[r][c];
                if ((c-1) == 0)
                {
                    loss += 1;
                }
            }
            if (move>95 && move<101)
            {
                grid2[r+1][c-1] += grid1[r][c];
                if (((r+1) == 22) || ((c-1) == 0))
                {
                    loss += 1;
                }
            }
            move = 0;
        }
    }
}

int checkSquares(int grid2[][22])
{
    int r,c, runs;
    int count = 0;
    
    for (r=1; r<22; r++)
    {
        for (c=1; c<22; c++)
        {
            if (grid2[r][c] == 0)
            {
                count++;
            }
        }
    }
    
    if (count > 299)
    {
        runs = 1;
    }
    else
    {
        runs = 0;
    }
    //cout << runs;
    return runs;
}

void getTotals(int jumps, int loss, int totalJ, int totalL)
{
    totalJ += jumps;
    totalL += loss;
}

void getMinMax (int jumps, int min, int max)
{
    if (jumps < min)
    {
        min = jumps;
    }
    if (jumps > max)
    {
        max = jumps;
    }
}

void resetGrid(int grid1[][22], int grid2[][22])
{
    int r,c;
    for (r=1; r<22; r++)
    {
        for (c=1; c<22; c++)
        {
            grid1[r][c] = grid2[r][c];
        }
    }
}

void zeroOut(int grid2[][22])
{
    int r,c;
    for (r=1; r<22; r++)
    {
        for (c=1; c<22; c++)
        {
            grid2[r][c] = 0;
        }
    }
}

void initialize(int grid1[][22])
{
    int r,c;
    for (r=1; r<22; r++)
    {
        for (c=1; c<22; c++)
        {
            grid1[r][c] = 1;
        }
    }
}

void displayInfo(int TOTALJ, int TOTALL, int min, int max)
{
    int avgJ, clearTime, avgL, maxTime, minTime;
    avgJ = TOTALJ / 500;
    clearTime = (TOTALJ * 8) / 5;
    maxTime = (max * 8) / 5;
    minTime = (min * 8) / 5;
    avgL = TOTALL / 500;
    
    cout << "The average number of jumps before fire: " << avgJ << endl;
    cout << "The amount of time needed to clear the area: " << clearTime << endl;
    cout << "The shortest time: " << minTime << endl;
    cout << "The longest time: " << maxTime << endl;
    cout << "The average number of fleas lost before fire: " << avgL << endl;
}
I don't see you seeding the random number generator anywhere. Add a call to srand() at the start of main().
http://www.cplusplus.com/reference/cstdlib/srand/
Yeah I did that. It didn't work so I took it out. It still did the same numbers every time just different ones than before.
Are you giving srand() the same seed every time?
I tried using the time(0) and time(NULL) ones
You can just wrap the rand functionality into a function.

1
2
3
4
5
6
7
8
int random(int min, int max) {
	static bool random = false;
	if(!random) {
		random = true;
		srand(time(0));
	}
	return min + (rand%(max-min+1));
}
> It didn't work so I took it out
Don't post code which is different from your own but does not reproduce the problem ( or has other problems )

Not using `srand()' is different than using `srand()' incorrectly


As I said in http://www.cplusplus.com/forum/beginner/124362/#msg676241 your loop in line 30 executes only in the first iteration.

However, you should seed the rng, otherwise the next time you run your program you'll get the same results.
Well I added runs = 0, jumps = 0, and loss = 0 right about my
 
while (runs != 1)

loop. My output goes like:

4
84
1
27
1
23
1
20
1
17

and so on. So jumps is 4 for the first run, but only one for all the rest of them. I'm very confused about why.
I meant right above*
You are accessing out of bounds in the `jumpFleas()' function
by instance grid2[r+1][c+1] += grid1[r][c];

> My output goes like:
¿what's supposed to be?


void getTotals(int, int, int, int);
your function has no effect

Also, I think that you need to `initialze()' `grid1'
Last edited on
Topic archived. No new replies allowed.