0
Please explain me the rand() and srand() function in c++?
1 Respuesta
+ 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
}