Random and Pseudo Random Numbers in C

What are Random and Pseudo-Random Numbers?

Random Numbers are numbers generated in such a way that each number has an equal probability of being chosen. True randomness is difficult to achieve in computing due to the deterministic nature of machines.

Pseudo-Random Numbers are numbers generated using deterministic algorithms that simulate randomness. While they are not truly random, they are sufficient for most applications.

The rand() Function

The rand() function is provided by the C standard library to generate pseudo-random numbers.

Syntax:

#include <stdlib.h>
int rand(void);

Example:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int mcq = rand();
    printf("Sanfoundry Random MCQ Number: %d\n", mcq);
    return 0;
}

Output:

advertisement
Random number: 1804289383

Note: Every time you run the program, you’ll get the same output unless you change the seed.

Seeding with srand()

To ensure different sequences of random numbers across program executions, seed the random number generator using srand().

Free 30-Day Python Certification Bootcamp is Live. Join Now!

Syntax:

#include <stdlib.h>
 
void srand(unsigned int seed);

Example:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main() {
    srand(time(NULL));  // Seed with current time
    int quiz = rand();
    printf("Quiz Random Seeded Value: %d\n", quiz);
    return 0;
}

Output:
(Random values that change every run)

Quiz Random Seeded Value: 1901938869
 
Quiz Random Seeded Value: 2007872022
 
Quiz Random Seeded Value: 1126734679

Seeding with the current time ensures a different sequence of random numbers each time the program runs.

Generating Random Numbers in a Range

To generate random numbers in a specific range using rand() in C, you can use this formula:

Syntax for Range [min, max]:

int randomNumber = min + rand() % (max - min + 1);

Example: Generate Sanfoundry Test Codes Between 1000 and 9999

advertisement
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main()
{
    int min = 1000, max = 9999;
 
    // Seed the random number generator
    srand(time(NULL));
 
    // Generate and print 3 Sanfoundry Test Codes
    for (int i = 0; i < 3; i++) {
        int testCode = min + rand() % (max - min + 1);
        printf("Sanfoundry Test Code %d: %d\n", i + 1, testCode);
    }
 
    return 0;
}

Sample Output (will vary each run):

Sanfoundry Test Code 1: 4831
Sanfoundry Test Code 2: 7904
Sanfoundry Test Code 3: 1235

This C program creates 3 random test codes between 1000 and 9999. It first seeds the random number generator using the current time so the numbers change each time you run it. Then, inside a loop, it uses rand() to generate a random number in that range. Each number is printed as a “Sanfoundry Test Code”. This is a simple way to make unique codes for tests.

rand_r() Function in C

rand_r() is a reentrant version of rand(), meaning it’s thread-safe. Unlike rand(), it requires an explicit seed to be passed as a pointer, and it doesn’t use global state.

Syntax:

int rand_r(unsigned int *seed);

seed: A pointer to an unsigned int used to hold the seed value (modified internally).

Example:

#include <stdio.h>
#include <stdlib.h>
 
int main() {
    unsigned int quizSeed = 12345;  // Custom seed
    int i;
 
    printf("Sanfoundry Quiz Random Numbers:\n");
    for (i = 0; i < 5; i++) {
        int quizRand = rand_r(&quizSeed);
        printf("Quiz %d: %d\n", i + 1, quizRand % 100);  // Range: 0–99
    }
 
    return 0;
}

Sample Output (Will be consistent each time unless the seed changes):

Sanfoundry Quiz Random Numbers:
Quiz 1: 44
Quiz 2: 82
Quiz 3: 35
Quiz 4: 12
Quiz 5: 4

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.