#include <stdlib.h> void srand(unsigned int seed);
srand
resets the number generator to a new starting point. The
rand
function then uses this seed
to generate a sequence of
pseudorandom numbers. The initial default seed
is 1.
srand
has no return value.
rand
for more information.
srand
to print 1,000 random numbers:
#include <stdio.h> #include <stdlib.h> main(int argc, char *argv[]) { int i, x; if (argc > 1){ x = atoi(argv[i]); if (x == 0){ x = 1; } printf("Seed value is %dn",x); srand(x); } puts("Here are 1000 random numbers:"); for (i = 0; i < 200; i++){ printf("%5d %5d %5d %5d %5dn", rand(),rand(),rand(),rand(),rand()); } puts("n"); }
rand
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.