C language random number function example

  • 2020-04-02 02:20:26
  • OfStack

void srand( unsigned int seed );
head file is < stdlib.h >
Remarks
The srand function sets the starting point for generating a series of pseudorandom
integers. To reinitialize the generator, use 1 as the seed argument. Any other value for
seed sets the generator to a random starting point. rand retrieves the pseudorandom
numbers that are generated. Calling rand before any call to srand generates the same
sequence as calling srand with seed passed as 1.


#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
   int i;
   
   srand((unsigned)time(NULL));
   
   for( i = 0;   i < 10;i++ )
      printf("  %6dn", rand());
}


Related articles: