Mega Code Archive

 
Categories / C / Code Snippets
 

Sets a starting point for the sequence generated by rand()

//Declaration: void srand(unsigned int seed); //Return: returns pseudorandom numbers. #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int j, stime; long ltime; /* get the current calendar time */ ltime = time(NULL); stime = (unsigned) ltime/2; srand(stime); for(j=0; j<10; j++) printf("%d ", rand()); return 0; }