# Thread: Random number with srand()

1. ## Random number with srand()

I am working on a math tutor program that will generate two random numbers and display the result. I believe I have to use the srand() function to generate random number. The book mentions a seed value which I am a very unclear on. Could I get an explanation on what a seed value is? Below is the source that I have got so far.

Code:
```#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int main()
{
int x,y,z;
char(ch);
x=rand();
y=rand();

z=x+y;

cout<<"What is the sum of: "<<x<<" and "<<y<<endl;

cout<<"Press a key of the answer!"<<endl;
cin.get(ch);

cout<<"The sum of: "<<x<<" and "<<y<<" is "<<z<<endl;

system("pause");

return 0;

}```

2. The source code is completely irrelevant to the question.
Refer to Random seed - Wikipedia, the free encyclopedia and come back with more specific question.

3. The seed value determines the initial state of the pseudorandom number generator (PRNG), which in turn determines which sequence of pseudorandom numbers is generated. Thus, you use srand() to seed and rand() to obtain the next number in the sequence. In your example, you do not use srand() at all, so the PRNG is seeded as if you wrote srand(1).

Also, read Prelude's article on using rand().

4. The easiest way of initializing the random number generator is basically

std::srand(std::time(nullptr));

Don't forget to include appropriate headers.
That said, it's still a good idea to read Prelude's article on rand.

5. ## Time as a seed

Thanks for the feedback..

Using the time function for my seed value.

srand(time(0));

6. Use nullptr, not 0.

7. Originally Posted by Elysia
The easiest way of initializing the random number generator is basically

std::srand(std::time(nullptr));

Don't forget to include appropriate headers.
That said, it's still a good idea to read Prelude's article on rand.
On a totally different topic, is she still writing those articles anymore?

8. Dunno. How about you ask her?

Popular pages Recent additions