C Reference function srand() initialize random number generator

This function of stdlib will initialize the random number generator that can be used with the rand() function.

Usage of srand():

void srand ( unsigned int seed );

The function srand() is used to initialize the pseudo-random number generator by passing the argument seed.
Often the function time is used as input for the seed.

If the seed is set to 1 then the generator is reinitialized to its initial value. Then it will produce the results as before any call to rand and srand.


An integer value to initialize the random number generator. Often the function time is used as input for the seed.

Return value:

There is nothing returned by srand.

Source code example of srand():


	int main ()
		printf ("Our first number: %d\n", rand() % 100);
		srand ( time(NULL) );

		printf ("Some random number: %d\n", rand() % 100);
		srand ( 1 );

		printf ("The first number again: %d\n", rand() %100);

		return 0;

Output of the srand example program above:

	Our first number: 41
	Some random number: 4
	The first number again: 41

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 4 responses to “C Reference function srand() initialize random number generator”

Why not let us know what you think by adding your own comment!

  1. Irene QIAN on March 23rd, 2013:

    #define ROW_SIZE 50
    #define COL_SIZE 100
    int i,j;
    int image[ROW_SIZE][COL_SIZE];
    for (i=0;i<ROW_SIZE;i++)
    }//end if

    } //end else

    printf("%d ", image[i][j]);
    }// end for j
    }// end for i

    }// end main

    I don't know how to use the srand()& rand(), so when I run it, only "0"

  2. Vikram Singh on May 7th, 2013:

    I got the ans

  3. Parker Lopuzzo on July 26th, 2013:

    Keep up the good work i will return often.

  4. Mantu Malakar(asansol,westbengal,9851299737) on April 21st, 2014:

    nice explanation …. keep the good works going this will help us a lot as a beginner programmer