When learning the C programming language, one of the most exciting things beginners discover is how to make their programs behave unpredictably — just like life itself! Random numbers bring that touch of surprise to your code. Whether you are building a game, simulating a dice roll, or testing algorithms, knowing how to generate random numbers in C is an essential skill every programmer should master.

with hands-on learning.
get the skills and confidence to land your next move.
In the real world, random numbers are everywhere — from shuffling cards in a game to generating secure passwords and encryption keys. Understanding how to use random numbers properly helps you write programs that are not only dynamic but also closer to real-world behavior. In this article, we’ll explore how to generate random numbers in C step-by-step, using simple examples that any beginner can follow.
Program 1: Generate a Simple Random Number
Our first program shows how to generate a basic random number using C’s built-in functions. This program is short, simple, and helps you understand the basic concept behind random number generation.
#include <stdio.h>
#include <stdlib.h>
int main() {
int randomNumber;
randomNumber = rand();
printf("Random Number: %d\n", randomNumber);
return 0;
}
This program uses the rand()
function from the C Standard Library. Every time you run the program, it will print a random number between 0 and a large positive number (usually 32767). However, if you run the program multiple times, you might notice that it shows the same random number each time. That’s because the rand()
function uses a fixed seed by default. Don’t worry — we’ll fix that in the next example.
Program 2: Generate a Different Random Number Each Time
To make your random numbers truly random each time the program runs, we need to change the seed value. This can be done using the srand()
function along with time(NULL)
from the time.h
library.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int randomNumber;
srand(time(NULL));
randomNumber = rand();
printf("Random Number: %d\n", randomNumber);
return 0;
}
Here, the srand()
function sets the seed for rand()
using the current time. Because time is always changing, the seed value becomes unique each time you run the program. As a result, the random number you get will be different on every run. This is very useful when writing programs like games or simulations, where you need a new random result every time.
Program 3: Generate Random Numbers Within a Range
Sometimes, you may want to generate random numbers within a specific range — for example, between 1 and 100. The following program shows how to do that easily.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int lower = 1, upper = 100;
int randomNumber;
srand(time(NULL));
randomNumber = (rand() % (upper - lower + 1)) + lower;
printf("Random Number between %d and %d: %d\n", lower, upper, randomNumber);
return 0;
}
In this program, we use the modulus operator %
to limit the range of the random number. The expression (rand() % (upper - lower + 1)) + lower
ensures that the random number will always fall between the lower and upper limits. Beginners often find this trick handy when creating random test data, picking lottery numbers, or simulating dice rolls.
Program 4: Generate Multiple Random Numbers
Sometimes one random number is not enough. You might want to generate a list of random numbers — for example, when simulating multiple dice throws or filling an array with random data. The next example shows how you can do this.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i;
srand(time(NULL));
printf("Five Random Numbers:\n");
for(i = 0; i < 5; i++) {
printf("%d ", rand());
}
printf("\n");
return 0;
}
This program uses a for
loop to generate five random numbers. Each number is printed in a single line, showing how quickly you can produce many random values. By understanding how loops work together with random number generation, you can easily expand your program to create more complex behaviors, such as random shuffling or selection.
Program 5: Random Numbers with Floating Points
Random numbers are not always integers. Sometimes, you may need random floating-point numbers for calculations or simulations. This example shows how to generate random floating numbers between 0 and 1.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
float randomValue;
srand(time(NULL));
randomValue = (float) rand() / RAND_MAX;
printf("Random Float: %f\n", randomValue);
return 0;
}
Here, RAND_MAX
is the maximum value rand()
can return. By dividing the random number by RAND_MAX
, we get a decimal number between 0 and 1. This method is useful in scientific simulations, games, and statistical models that require decimal-based randomness.
Frequently Asked Questions (FAQ)
When learning to generate random numbers in C, beginners often have common questions. Let’s answer a few of them to make your journey smoother.
Q1. Why do I get the same random number every time I run my program?
That happens because the random number generator uses the same seed value each time. To fix this, use srand(time(NULL));
before calling rand()
.
Q2. What is the range of numbers produced by rand()
?
The range is from 0 to RAND_MAX
. The actual value of RAND_MAX
depends on your system, but it’s usually 32767.
Q3. Can I generate negative random numbers?
Yes, you can. You can simply subtract or adjust your range to include negative values, for example:int randomNumber = (rand() % 21) - 10;
to get numbers between -10 and 10.
Q4. Is rand()
good enough for cryptography?
No, rand()
is not suitable for cryptographic use because it’s predictable. For security purposes, you should use more advanced libraries or algorithms that provide stronger randomness.
Conclusion
Generating random numbers in C is a fun and practical way to bring your programs to life. Whether you’re building a simple game, testing your logic, or simulating real-world events, random numbers can make your code dynamic and engaging. Once you understand how seeding works and how to limit your ranges, you’ll find endless ways to use randomness in your projects. Keep experimenting and try combining these programs to create your own exciting applications.
Additional & References
If you want to learn more about how random number generation works in C and explore advanced uses, these resources will help deepen your understanding.
- C Standard Library Documentation (stdlib.h) – Official reference for the
rand()
andsrand()
functions. Perfect for understanding how they behave in different environments. - GeeksforGeeks C Programming Tutorials – Beginner-friendly tutorials with practical examples and exercises to help you practice coding.
- Programiz C Language Guide – A clear, structured guide with step-by-step explanations of C programming basics, including random numbers.
- Learn-C.org – Interactive platform for trying out C programs online, great for testing your random number code easily.