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);
Call
What 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
Parameter
Description
seed
Value that selects the rand() pseudo-random sequence.
Return Value
Return Value
Description
No value
srand() 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;
}
Output
in range: yes
How It Works
Step 1. Initialize the sequence with a fixed seed.
Step 2. Call rand() to advance and obtain a value.
Step 3. Compare against the standard inclusive bounds.
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");}
Output
same: yes
How It Works
Step 1. Seed the generator with a fixed test value.
Step 2. Save the first pseudo-random result.
Step 3. Reset with exactly the same seed.
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");}
Output
valid die: yes
How It Works
Step 1. Choose one deterministic seed for the example.
Step 2. Reduce a rand() result to six buckets.
Step 3. Add one to shift the displayed range to 1 through 6.
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.