srand() function
Description
The C library function void srand(unsigned int seed) seeds the random number generator used by the function rand.
Declaration
Following is the declaration for srand() function.
void srand(unsigned int seed)
Parameters
seed -- This is an integer value to be used as seed by the pseudo-random number generator algorithm.
Return Value
This function does not return any value.
Example
The following example shows the usage of srand() function.
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int i, n;
time_t t;
n = 5;
/* Intializes random number generator */
srand((unsigned) time(NULL));
/* Print 5 random numbers from 0 to 50 */
for( i = 0 ; i < n ; i++ ) {
coutrand() % 50"\n";
}
return(0);
}
return(0);
}
Sample Result
38
45
29
29
47