0
Can anyone explain me why srand() is used and what is difference between srand() and rand()??
2 ответов
0
Both are to generate random numbers, but if you use rand() alone, each time you call, it generated the same number every time in your program. But if you call srand() (which set seed for rand() function) before rand(), different series of random numbers will be generated. Remember that you should set seed for srand().
rand() returns numbers between 0 and RAND_MAX means: 0 to 32767
Example:
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand(time(0));
cout<<rand();
}
*** time(0) gives you time in seconds
0
Thxs sir