The C rand() function produces a pseudo-random integer. Each call advances a deterministic library sequence. srand() selects its starting seed; the default behaves as if srand(1) had been called.
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. Every call returns an integer in the inclusive range 0 through RAND_MAX. rand() has no failure sentinel; its limitations concern quality and predictability. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete rand() call:
C
rand();
Call
What it does
rand();
Produces a pseudo-random integer 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
int rand(void);
Parameters
Parameter
Description
None
rand() takes no parameters.
Return Value
Return Value
Description
0 through RAND_MAX
A pseudo-random integer from the implementation's sequence.
C Examples
Example 1 — Generate a bounded demonstration value
This complete program demonstrates a distinct part of the rand() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void){
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 using the library default.
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 rand() 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 rand() 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
rand() produces a pseudo-random integer. Each call advances a deterministic library sequence. srand() selects its starting seed; the default behaves as if srand(1) had been called.
Include <stdlib.h> before calling rand().
Every call returns an integer in the inclusive range 0 through RAND_MAX. rand() has no failure sentinel; its limitations concern quality and predictability.
rand() is predictable and unsuitable for passwords, tokens, keys, or security decisions. Modulo reduction also introduces bias unless the range divides RAND_MAX + 1.
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.