C srand() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C srand() function seeds the rand pseudo-random sequence. The same seed reproduces the same implementation-specific sequence, which is useful for repeatable tests. Different C libraries may produce different sequences.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. A call deterministically resets the sequence for subsequent rand() calls. There is no failure return; seed quality affects repetition, not call success. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete srand() call:

C
srand(1234u);
CallWhat it does
srand(1234u);Seeds the rand pseudo-random sequence using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
void srand(unsigned int seed);

Parameters

ParameterDescription
seedValue that selects the rand() pseudo-random sequence.

Return Value

Return ValueDescription
No valuesrand() returns void.

C Examples

Example 1 — Generate a bounded demonstration value

This complete program demonstrates a distinct part of the srand() contract and checks the result or precondition that matters for the scenario.

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

int main(void){
    srand(1234u);
    int value=rand();
    printf("in range: %s\n",value>=0&&value<=RAND_MAX?"yes":"no");
    return 0;
}

How It Works

  1. Step 1. Initialize the sequence with a fixed seed.
  2. Step 2. Call rand() to advance and obtain a value.
  3. Step 3. Compare against the standard inclusive bounds.
  4. Step 4. Report the range property rather than a library-specific number.

Example 2 — Repeat a test sequence

This complete program demonstrates a distinct part of the srand() contract and checks the result or precondition that matters for the scenario.

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

int main(void){srand(7u);int first=rand();srand(7u);int again=rand();printf("same: %s\n",first==again?"yes":"no");}

How It Works

  1. Step 1. Seed the generator with a fixed test value.
  2. Step 2. Save the first pseudo-random result.
  3. Step 3. Reset with exactly the same seed.
  4. Step 4. The first result of that implementation's sequence repeats.

Example 3 — Roll a simple practice die

This complete program demonstrates a distinct part of the srand() contract and checks the result or precondition that matters for the scenario.

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

int main(void){srand(25u);int die=1+rand()%6;printf("valid die: %s\n",die>=1&&die<=6?"yes":"no");}

How It Works

  1. Step 1. Choose one deterministic seed for the example.
  2. Step 2. Reduce a rand() result to six buckets.
  3. Step 3. Add one to shift the displayed range to 1 through 6.
  4. Step 4. Use this only for simple simulations, not security or perfectly unbiased sampling.

Frequently Asked Questions

srand() seeds the rand pseudo-random sequence. The same seed reproduces the same implementation-specific sequence, which is useful for repeatable tests. Different C libraries may produce different sequences.
Include <stdlib.h> before calling srand().
A call deterministically resets the sequence for subsequent rand() calls. There is no failure return; seed quality affects repetition, not call success.
Seed once near program startup, not before every rand() call. A time-based seed is still not cryptographically secure.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: strtod()

Continue through the C stdlib.h tutorials.

strtod() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful