Introduction
In many programming scenarios, you may need to generate random numbers, whether for simulations, games, random sampling, or other purposes. The C programming language provides the rand() function, which generates a pseudo-random integer between 0 and RAND_MAX. You can combine rand() with the modulus operator (%) to limit the random number within a specific range. To get different random sequences each time the program runs, you can use the srand() function with a seed value, typically the current time, obtained using the time() function.
Example:
- Generate 5 Random Numbers between 0 and 99: The program will generate five random integers within this range and display them.
Problem Statement
Create a C program that:
- Generates a specified number of random integers.
- Allows the user to specify the range of the random numbers.
- Outputs the generated random numbers.
Solution Steps
- Include the Standard Libraries: Use
#include <stdio.h>for standard input-output functions,#include <stdlib.h>for random number functions, and#include <time.h>for seeding the random number generator. - Implement the Random Number Generation:
- Use
rand()to generate random numbers. - Use
srand()withtime(NULL)to seed the random number generator. - Limit the random numbers to a specific range using the modulus operator.
- Create a Main Function: Allow the user to input the range and number of random numbers to generate, and display the generated random numbers.
C Program to Generate Random Numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int lower, upper, count;
// Input the range and number of random numbers
printf("Enter the lower bound of the range: ");
scanf("%d", &lower);
printf("Enter the upper bound of the range: ");
scanf("%d", &upper);
printf("Enter the number of random numbers to generate: ");
scanf("%d", &count);
// Seed the random number generator with the current time
srand(time(0));
// Generate and print the random numbers
printf("Random numbers between %d and %d:\n", lower, upper);
for (int i = 0; i < count; i++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("%d\n", num);
}
return 0; // Return 0 to indicate successful execution
}
Explanation
Seeding the Random Number Generator
srand(time(0)): This function seeds the random number generator with the current time. This ensures that each time the program runs, a different sequence of random numbers is generated.
Generating Random Numbers
rand(): This function generates a pseudo-random number.- Range Limitation: To generate a number within a specific range, use the formula:
(rand() % (upper - lower + 1)) + lower. This ensures that the generated number lies betweenlowerandupper.
Main Function
- The
mainfunction takes user inputs for the range (lowerandupper) and the number of random numbers to generate (count). - It then generates and prints the specified number of random numbers within the given range.
Output Example
Example Output:
Enter the lower bound of the range: 10
Enter the upper bound of the range: 50
Enter the number of random numbers to generate: 5
Random numbers between 10 and 50:
33
47
12
22
45
Example Output (Different Range):
Enter the lower bound of the range: 1
Enter the upper bound of the range: 100
Enter the number of random numbers to generate: 3
Random numbers between 1 and 100:
72
14
88
Conclusion
This C program demonstrates how to generate random numbers within a specified range. By using the rand() and srand() functions, you can generate pseudo-random numbers that vary with each execution of the program. This approach is useful in various applications, including games, simulations, and statistical sampling. The program provides a clear example of how to implement random number generation in C programming.