Chapter Contents

Previous

Next
srand

srand



Initialize Random Number Generator

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdlib.h>

void srand(unsigned int seed);


DESCRIPTION

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.


RETURN VALUE

srand has no return value.


PORTABILITY

See the portability details for rand for more information.


EXAMPLE

This example uses 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 %d\n",x);
      srand(x);
   }
   puts("Here are 1000 random numbers:");
   for (i = 0; i < 200; i++){
      printf("%5d %5d %5d %5d %5d\n",
             rand(),rand(),rand(),rand(),rand());
   }
   puts("\n");
}


RELATED FUNCTIONS

rand


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.