1. Introduction
The C programming language is widely used in low-level system programming and game development. Among its many applications, “random number generation” is frequently utilized. For example, it is used to create random elements in games, run simulations, perform encryption, and generate test data, among many other uses.
This article explains how to generate random numbers within a specified range in C, using concrete examples. It is designed to help readers understand the mechanics of random number generation and apply them in real-world programs.
2. Basics of Random Number Generation in C
In C, random numbers are generated using the standard library <stdlib.h>
. The rand()
function produces integer random numbers in the range from 0 to RAND_MAX
(approximately 2147483647). However, if you use the same seed value, the same sequence of random numbers will be generated each time.
Generating Pseudo-Random Numbers and Setting the Seed Value
An important concept in random number generation is the “seed value.” The seed determines the starting point of the random number sequence. Without setting it, the program will generate the same numbers each time it runs. To avoid this, use the srand()
function to set the seed. Typically, the time()
function is used to set the current time as the seed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned int)time(NULL));
printf("%d\n", rand());
return 0;
}
This ensures that different random numbers are generated each time the program runs.
3. Generating Random Numbers Within a Specified Range
If you want a random number within a specific range, you need to apply mathematical operations to the result of the rand()
function. The basic formula for generating a random number within a range is:
rand() % (max - min + 1) + min
This generates an integer random number between min
and max
(inclusive).
Sample Code: Random Number Generation Within a Range
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int min = 1;
int max = 100;
srand((unsigned int)time(NULL));
int random_number = rand() % (max - min + 1) + min;
printf("Random number between %d and %d: %d\n", min, max, random_number);
return 0;
}
This code generates and prints a random number between 1 and 100.
4. Generating Random Floating-Point Numbers
To generate floating-point random numbers, use the rand()
function with RAND_MAX
for scaling. To produce a random number between 0.0 and 1.0, use the following code:
Sample Code: Random Floating-Point Number Generation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned int)time(NULL));
double random_number = (double)rand() / RAND_MAX;
printf("Random number between 0.0 and 1.0: %f\n", random_number);
return 0;
}
This program generates random numbers in the range 0 to 1. By applying scaling, you can generate random numbers in any floating-point range. For example, to get numbers between 0.0 and 5.0, multiply the result by 5.

5. Practical Applications of Random Numbers
Dice Simulation
In game development, dice simulations are a common example of random number usage. The following code generates a random number between 1 and 6 to simulate a dice roll:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned int)time(NULL));
int dice_roll = rand() % 6 + 1;
printf("Dice roll: %d\n", dice_roll);
return 0;
}
Approximating Pi Using the Monte Carlo Method
The Monte Carlo method uses random numbers to approximate solutions to problems that are difficult to solve analytically. The following code demonstrates approximating π using random numbers:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n_trials = 1000000;
int n_inside = 0;
double x, y, pi;
srand((unsigned int)time(NULL));
for (int i = 0; i < n_trials; i++) {
x = (double)rand() / RAND_MAX;
y = (double)rand() / RAND_MAX;
if (x * x + y * y <= 1) {
n_inside++;
}
}
pi = 4.0 * n_inside / n_trials;
printf("Approximated π: %f\n", pi);
return 0;
}
This method calculates π by generating random numbers and evaluating how many fall within a unit circle. It is widely used in physics simulations and probabilistic modeling.
6. Conclusion
In this article, we covered how to generate random numbers within a specified range in C. We discussed the basic usage of rand()
and srand()
, generating numbers in both integer and floating-point ranges, and explored practical applications like dice simulations and the Monte Carlo method. Random number generation is an essential technique in many fields, including game development and simulations.
Try applying these techniques to your future projects to enhance functionality and variability.