# Thread: Need Help Generating Random Numbers In C++

1. ## Need Help Generating Random Numbers In C++

Okay dudes, I have this program and when I run it I need it to generater random numbers, but I can't seem to figure out how to use the srand() thing to do it. Thank for the help in advance.

Code:
```#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{

bool TrueFalse1, TrueFalse2, TrueFalse3, TrueFalse4, TrueFalse5;
long int xNumber = 1 + rand() % 50,
yNumber = 1 + rand() % 50,
A = 1 + rand() % 50,
B = 1 + rand() % 50,
C = 1 + rand() % 50,
result1, result2, result3, result4, result5,
result,
total,
x = xNumber,
y = yNumber;

cout<<"Solve the following expression where x = "<<xNumber << " and y = "<<yNumber<<endl;
cout<<A<<" x + "<<B<<" y + "<<C<<endl;
cin>> result1;
answer1 = (A * x) + (B * y) + C;

else

cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
cout<<A<<" x + "<<B<<" y + "<<C<<endl;
cin>> result2;
answer2 = (A * x) + (B * y) + C;

else

cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
cout<<A<<" x + "<<B<<" y + "<<C<<endl;
cin>> result3;
answer3 = (A * x) + (B * y) + C;

else

cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
cout<<A<<" x + "<<B<<" y + "<<C<<endl;
cin>> result4;
answer4 = (A * x) + (B * y) + C;

else

cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;
cout<<A<<" x + "<<B<<" y + "<<C<<endl;
cin>> result5;
answer5 = (A * x) + (B * y) + C;

else

total = TrueFalse1 + TrueFalse2 + TrueFalse3 + TrueFalse4 + TrueFalse5;
return 0;

}```

2. Code:
```#include <time.h>
#include <stdlib.h>

srand(time(NULL));```
Call it once at the begine of your program.
These basic questions are covered in the FAQ

3. Thanks for the advice, I look into the faq, but I could not solve the problem (I got the same values every time I ran the programm). I am a real Newbie to programming in C++. Could someone please help me a litter further.

4. Put your current program aside, and come up with the smallest and simplest program that has this problem. An example would be a program that prints just 5 pseudo-random numbers. Run it a few times, and see if you still get the same values every time the program is run.

6. I don't know where to put the srand(), so it does not show the same numbers everytime. Here is the simple random number program, but I don't know how to make it so a user does not have to enter a seed number in order of it to generate random number and how can a generate a random number to a defined variable that equals a random number.

Code:
```#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
unsigned seed,y;
cout<< "Enter seed\n";
cin >> seed;
srand(seed);

y=1+rand()%50;
cout<<y<<endl;
return 0;

}```

7. Try this:

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
srand(time(0));

cout << (rand() % 100) << endl;

return 0;
}```

8. > I don't know where to put the srand()
The FAQ seemed pretty clear to me!

"If you want to get different random numbers each time you run your program, you will also need to use the srand() function"

"To get different random number set, pass a different seed. The most common way to achieve this is by calling the time() function and passing its result to srand()"

And there's an example right there, showing
srand(time(NULL));

9. you also might try a different function set, rand isn't exactly the best function out there.

10. What's a better one?

11. Thanks guys I got it to work!

12. Be sure to cast time(0) to what ever type srand takes.