• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

AHHH why doesnt this work! C++ question

brtspears2

Diamond Member
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);
}
}
 
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().
 
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.
 
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.
 


<< 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.
 
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.
 
Back
Top