srand - C stdlib.h

C examples for stdlib.h:srand

Type

function

From


<cstdlib>
<stdlib.h>

Description

Initialize random number generator

Prototype

void srand (unsigned int seed);

Parameters

Parameter Description
seed An integer value to be used as seed by the pseudo-random number generator algorithm.

Return Value

none

Demo Code


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()// www  . ja v a2 s .c  o m
{
  printf ("First number: %d\n", rand()%100);
  srand (time(NULL));
  printf ("Random number: %d\n", rand()%100);
  srand (1);
  printf ("Again the first number: %d\n", rand()%100);

  return 0;
}

Related Tutorials