rand() Function
The rand()
function generates a pseudo-random integer within a predetermined range. It produces a sequence of seemingly unrelated numbers each time it is called, making it useful for various applications such as simulations and games. To ensure variability in the sequence across different runs, it is common to initialize the random number generator with a seed value using srand()
.
Syntax of rand()
int rand(void);
Parameters
Parameter | Description |
---|---|
None | This function does not accept any parameters. |
The generated number is produced by an internal algorithm that can be seeded using srand()
with a unique value (such as the current time) to ensure different sequences on each execution. When using rand()
to limit the output to a specific range, a modulo operation is often applied; however, this may result in a non-uniform distribution.
Return Value
The function returns an integer value between 0 and RAND_MAX
.
Examples for rand()
Example 1: Generating a Single Random Number
This example demonstrates the basic usage of rand()
to generate and print a single pseudo-random number.
Program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int num = rand();
printf("Random number: %d\n", num);
return 0;
}
Explanation:
- The program includes the necessary headers for input/output and standard library functions.
rand()
is called to generate a pseudo-random integer.- The generated number is printed to the console.
- Since
srand()
is not used, the sequence of numbers will be the same for every execution.
Output:
Random number: 1804289383
Example 2: Generating a Random Number Within a Specific Range
This example shows how to generate a random number within a defined range (1 to 100) using the modulo operator and an offset.
Program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int num = rand() % 100 + 1;
printf("Random number (1-100): %d\n", num);
return 0;
}
Explanation:
- The program generates a pseudo-random number using
rand()
. - The modulo operator restricts the result to the range 0-99, and adding 1 shifts it to 1-100.
- The resulting random number is printed to the console.
Output:
Random number (1-100): 57
Example 3: Seeding the Random Number Generator for Varied Output
This example demonstrates how to seed the random number generator with the current time to ensure a different sequence of random numbers on each execution.
Program
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
// Seed the random number generator with the current time
srand(time(NULL));
// Generate and print five random numbers
for (int i = 0; i < 5; i++) {
printf("Random number %d: %d\n", i + 1, rand());
}
return 0;
}
Explanation:
- The program seeds the random number generator using
srand()
with the current time obtained fromtime(NULL)
. - A loop is used to generate and print five pseudo-random numbers.
- Each call to
rand()
produces a different number in the sequence, resulting in varied output on each execution.
Output:
Random number 1: 725267989
Random number 2: 1718491175
Random number 3: 1296731123
Random number 4: 503065708
Random number 5: 1875767393