0

Can anyone explain me why srand() is used and what is difference between srand() and rand()??

14th Apr 2020, 12:16 PM
Sameer Sowrab
Sameer Sowrab - avatar
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
14th Apr 2020, 1:24 PM
Saman Rezaei
Saman Rezaei - avatar
0
Thxs sir
17th Apr 2020, 6:34 AM
Sameer Sowrab
Sameer Sowrab - avatar