# Need Help Generating Random Numbers In C++

• 09-18-2005
slickwilly440
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,                 answer1, answer2, answer3,answer4,answer5,                 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;         cout<<"Write your result:\n";         cin>> result1;         answer1 = (A * x) + (B * y) + C;                         if (result1 == answer1)                 cout<<"Your answer is correct\n";         else                 cout<<"Your answer is wrong. The correct answer is "<<answer1<<endl;         TrueFalse1 = (result1 == answer1);                 cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;         cout<<A<<" x + "<<B<<" y + "<<C<<endl;         cout<<"Write your result:\n";         cin>> result2;         answer2 = (A * x) + (B * y) + C;                 if (result2 == answer2)                 cout<<"Your answer is correct\n";         else                 cout<<"Your answer is wrong. The correct answer is "<<answer2<<endl;         TrueFalse2 = (result2 == answer2);                 cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;         cout<<A<<" x + "<<B<<" y + "<<C<<endl;         cout<<"Write your result:\n";         cin>> result3;         answer3 = (A * x) + (B * y) + C;                 if (result3 == answer3)                 cout<<"Your answer is correct\n";         else                 cout<<"Your answer is wrong. The correct answer is "<<answer3<<endl;         TrueFalse3 = (result3 == answer3);                 cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;         cout<<A<<" x + "<<B<<" y + "<<C<<endl;         cout<<"Write your result:\n";         cin>> result4;         answer4 = (A * x) + (B * y) + C;         if (result4 == answer4)                 cout<<"Your answer is correct\n";         else                 cout<<"Your answer is wrong. The correct answer is "<<answer4<<endl;         TrueFalse4 = (result4 == answer4);                         cout<<"Solve the following expression where x = "<<xNumber<<" and y = "<<yNumber<<endl;         cout<<A<<" x + "<<B<<" y + "<<C<<endl;         cout<<"Write your result:\n";         cin>> result5;         answer5 = (A * x) + (B * y) + C;         if (result5 == answer5)                 cout<<"Your answer is correct\n";         else                 cout<<"Your answer is wrong. The correct answer is "<<answer5<<endl;         TrueFalse5 = (result5 == answer5);         total = TrueFalse1 + TrueFalse2 + TrueFalse3 + TrueFalse4 + TrueFalse5;         cout<< "You answered "<<total<<" correctly.\n";         return 0; }```
• 09-18-2005
sand_man
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
• 09-18-2005
slickwilly440
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.
• 09-18-2005
laserlight
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.
• 09-18-2005
Salem
• 09-18-2005
slickwilly440
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; }```
• 09-18-2005
dwks
Try this:

Code:

```#include <iostream> #include <cstdlib> #include <ctime> int main() {     srand(time(0));     cout << (rand() % 100) << endl;     return 0; }```
• 09-18-2005
Salem
> 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));
• 09-18-2005
valis
you also might try a different function set, rand isn't exactly the best function out there.
• 09-18-2005
dwks
What's a better one?
• 09-18-2005
slickwilly440
Thanks guys I got it to work!
• 09-18-2005
orbitz
Be sure to cast time(0) to what ever type srand takes.