Seeding the Random Number Generator - C++ Data Type

C++ examples for Data Type:Random

Description

Seeding the Random Number Generator

Demo Code

#include <iostream> 
#include <time.h> 
#include <stdlib.h> 

using namespace std; 

int main() /*  w w w.j a v  a2 s .com*/
{ 
    // Re-seed the random-number generator 
    time_t now; 
    time(&now); 
    srand(now); 
    rand(); 

    // Print out a list of random numbers 
    for (int i=0; i<5; i++){ 
        cout << rand() % 100 << endl; 
    } 

    return 0; 
}

Result


Related Tutorials