AHHH why doesnt this work! C++ question

brtspears2

Diamond Member
Nov 16, 2000
8,660
1
81
This program needs to return a specifed number of math programs...
But, when I specify a certain number of problems, it will just print out the same problem the specifed amount of times?

Any help? Perhaps I need to set return types?





#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

void numbers(int &, int& );
void output(int, int);
void control(int, int &, int &);

void main()
{
int times=0;
int num1=0, num2=0;

control(times, num1, num2);

}

void numbers(int &x, int &y)
{
srand(time(0));
x = (1 + rand()%1000);
y = (1 + rand()%1000);
}

void output(int x, int y)
{
cout << x << " + " << y << " = " << endl;
}

void control(int x, int &y, int &z)
{
cout << "How many problems to create? " << endl;
cin >> x;

for(int i=1; i<=x; i++)
{
numbers(y, z);
output(y,z);
}
}
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
You're passing a seed value to the random number generator multiple times. Because the program is running so quickly, the time being passed to it is the same at each call, so the random number generated immediately following it will always be the same. Move the srand() call outside of numbers() or create another function that calls srand() before you call control() in main().
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
Not really. You shouldn't be seeding the random number generator multiple times anyway. Each pseudo-random number is generated based upon the previous one. The pseudo-random property is lost by multiple seed values.

And you're welcome.
 

brtspears2

Diamond Member
Nov 16, 2000
8,660
1
81
Thanks, but, no matter how many times I seed it, it comes out the same...

void time()
{
srand(time(NULL));
}

I call this function a few times, and still will get the same output each time.
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0


<< Move the srand() call outside of numbers() or create another function that calls srand() before you call control() in main(). >>


Translation: Seed the random number generator ONCE and do everything else. DO NOT make multiple calls to srand() in your program. It should only be called ONCE.
 

stonecold3169

Platinum Member
Jan 30, 2001
2,060
0
76
after you've planted the seed, don't you still need to call randomize? Otherwise don't you always get the same?
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
Most random number generators use the previous value to generate the next number. This is why you only want to use one seed value.

Let's say our random number generator looks like this: X = C + 1, where C is the previous value generated or the seed value, and X is the random number.

In brtspears2's case, he kept on seeding the random number generator using a call to time(0). However, because the program was running so quickly, time(0) returned the same value every time. What happens if you did the same mathematical operation on the same number every time? You get the same value.

You can't guarantee that subsequent calls to time(0) will return a different number because you don't know how fast the program will be running. This is why you don't want to seed it multiple times.