srand() Function

The srand() function initializes the pseudo-random number generator used by rand(). Calling this function sets the starting point for producing a series of pseudo-random numbers, ensuring that different seed values result in different sequences while using the same seed will reproduce the same sequence of numbers.


Syntax of srand()

</>
Copy
void srand(unsigned int seed);

Parameters

ParameterDescription
seedAn integer value used to initialize the pseudo-random number generator.

Return Value

The srand() function does not return a value.

Note that the sequence of numbers generated by subsequent calls to rand() depends entirely on the seed provided. Using a runtime-based seed, such as the current time from time(NULL), ensures different outcomes on each execution, whereas a fixed seed will always produce the same sequence of numbers.


Examples for srand()

Example 1: Initializing with time() for a Varying Random Sequence

In this example, we will use srand() function with current time as seed, so that it generates random numbers that vary with the seed.

Program

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

int main() {
    // Initialize random number generator with current time
    srand(time(NULL));
    
    // Generate and print 5 random numbers
    for (int i = 0; i < 5; i++) {
        printf("Random number %d: %d\n", i + 1, rand());
    }
    return 0;
}

Explanation:

  1. The program includes the necessary headers for standard I/O, standard library functions, and time manipulation.
  2. srand() is called with the value returned by time(NULL) to seed the random number generator with the current time.
  3. A loop generates and prints five random numbers using rand(), ensuring different sequences on each run.

Program Output:

Random number 1: 878393937
Random number 2: 534544408
Random number 3: 747101208
Random number 4: 124719913
Random number 5: 1333526370

Example 2: Using a Fixed Seed to Reproduce the Same Random Sequence

In this example, we will use srand() function with current time as seed, so that it generates random numbers that vary with the seed.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    // Initialize random number generator with a fixed seed
    srand(1);
    
    // Generate and print 5 random numbers
    for (int i = 0; i < 5; i++) {
        printf("Random number %d: %d\n", i + 1, rand());
    }
    return 0;
}

Explanation:

  1. The program initializes the random number generator using a fixed seed by calling srand(1).
  2. This fixed seed ensures that rand() produces the same sequence of numbers every time the program is executed.
  3. A loop is used to generate and print five random numbers, demonstrating the reproducibility of the sequence.

Program Output:

Random number 1: 1804289383
Random number 2: 846930886
Random number 3: 1681692777
Random number 4: 1714636915
Random number 5: 1957747793

Whatever the number of times you run this program, you get the same sequence of random numbers, because the seed being same.