This is a C++ Program to make use of Rand and Srand functions to generate unique random numbers.
Here is source code of the C++ Program to Use rand and srand Functions. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char **argv)
{
cout << "First number: " << rand() % 100;
srand(time(NULL));
cout << "\nRandom number: " << rand() % 100;
srand(1);
cout << "\nAgain the first number: " << rand() % 100;
return 0;
}
Output:
$ g++ RandSrandFunctions.cpp $ a.out First number: 41 Random number: 98 Again the first number: 41
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.
Related Posts:
- Check C++ Books
- Apply for C++ Internship
- Practice Programming MCQs
- Check Programming Books
- Check Computer Science Books