| 1. |
Explain about generating random numbers with suitable program. |
|
Answer» The srand() function in C++ seeds the pseudo random number generator used by the rand() function. The seed for rand() function is 1 by default. It means that if no srand() is called before rand(), the rand() function behaves as if it was seeded with srand( 1). The srand() function takes an unsigned integer as its parameter which is used as seed by the rand() function. It is defined inor header file. #include #include using namespace std; int main() { int random = rand(); /* No srand() calls before rand(), so seed = 1*/ cout << “\nSeed = 1, Random number = ” << random; srand(10); /* Seed= 10 */ random = rand(); cout << “\n\n Seed =10, Random number = ” << random; return 0; } Output: Seed = 1, Random number = 41 Seed =10, Random number 71 |
|