+ 2
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to randor srand. In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime>). Example... #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { cout << "First number: " << rand() % 100 << endl; srand(time(NULL)); cout << "Random number: " << rand() % 100 << endl; srand(1); cout << "Again the first number: " << rand() % 100 << endl; return 0; } Output... First number: 41 Random number: 13 Again the first number: 41
9th Jul 2016, 4:59 PM
Albert Gullbee