0

Please explain me the rand() and srand() function in c++?

26th Sep 2017, 5:55 AM
Prateek Gupta
Prateek Gupta - avatar
2 Réponses
+ 20
As a brief description, rand() function from cstdlib library, provides you some facilities to generate "pseudo-random" numbers. (This means every time you'd run your program it's gonna generate the same number). As a good solution (not perfect) for this problem, srand() and time() functions go hand in hand to seed your random number generator (As a result, each time you run your program it will generate random numbers based on current system time). Here is a code fragment for illustrating the point: #include <cstdlib> #include <ctime> int main() { srand((size_t)time(0)); // seeder int nRand = rand() % 21; // generator }
26th Sep 2017, 6:28 AM
Babak
Babak - avatar