Home »
C solved programs »
C basic programs
C program to generate random numbers within a range
Generating random numbers in C: Here, we are going to learn how to generate random numbers within a given range in C programming language?
Submitted by Shivang Yadav, on September 16, 2019
Generating random number in a range in C
Random numbers just numbers that lie within a range and any of the numbers can occur.
In programming, we come through a lot of scenarios where we need to generate random numbers. Like for dice games, lottery system, etc. In the c programming language, there is an inbuilt method to take care of this.
Functions Used
srand() and rand() functions are used to generate random numbers. Now, let's dig deep into these methods and understand their working.
Generate random numbers using rand() function
The rand() function in the C programming language is used to generate a random number. The return type is of rand() function is an integer.
C code to generate a random number
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Random number is: %d ", rand());
return 0;
}
Output
Random number is: 1804289383
Generate random numbers using srand() function
The srand() function is used to set the starting value for the series of random integers. You can use the srand() to set the seed of the rand function to a different starting point.
The parameters that are passed into the srand() function are the starting values for the rand method.
C code to generate random numbers using srand()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(0));
printf("%d ", rand());
return 0;
}
Output
1370632410
Generating random numbers within a range
The rand() function generates random numbers that can be any integer value. But, to generate random numbers within a specific range, we have a formula that returns a random number between given ranges.
Formula:
number = (rand() % (upper - lower + 1)) + lower
Program to generate random numbers from 1 to 6
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int lower = 1, upper = 6, count = 10;
srand(time(0));
printf("The random numbers are: ");
for (int i = 0; i < count; i++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("%d ", num);
}
return 0;
}
Output
The random numbers are: 1 5 4 4 4 2 3 2 4 3
Explanation:
This program declares a variable num that generates a random number between 1 to 6. for doing this we will initialize the lower limit of the rand to 1 and The upper limit of the rand function to 6. this will generate a random number between 1 to 6 using the formula.
Program to generate random number between 1 to 100
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int lower = 1, upper = 100, count = 10;
srand(time(0));
printf("The random numbers are: ");
for (int i = 0; i < count; i++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("%d ", num);
}
return 0;
}
Output
The random numbers are: 50 22 52 69 24 98 39 31 77 97
C Basic Programs »