C rand() Function

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

Definition

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();
CallWhat 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

ParameterDescription
Nonerand() takes no parameters.

Return Value

Return ValueDescription
0 through RAND_MAXA 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;
}

How It Works

  1. Step 1. Initialize the sequence using the library default.
  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 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");}

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 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");}

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

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.

Next: realloc()

Continue through the C stdlib.h tutorials.

realloc() 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