C Program to Generate Random numbers within a Range2 min read

In this tutorial, you will learn to generate random numbers within a range in C programming. But before that, you may go through the topic below if you do not know about the rand() and srand() function used in random numbers generation.

Here we will use srand() function provided in C to generate random numbers. The current time will be used to seed the srand() function.

C Program to generate random numbers within the range

Since the rand() will generate random numbers between 0 to max value, but instead of 0 we want to enter the lower range as well as the upper range so here is the trick we need to implement.

(upRange - lrRange + 1)) then add the lower range at the end.

Output:

Enter how many random numbers you want: 7
Enter the lower range: 10
Enter the upper range: 50

7 random numbers are:
22 29 24 33 18 41 11

Again execute the program but this time entered for 10 random numbers, start from 1 and give upper range 7.

Enter how many random numbers you want: 10
Enter the lower range: 1
Enter the upper range: 7

7 random numbers are:
1 3 7 7 5 2 1 5 1 2

Notice that the program is repeating the number as you requested for 10 random numbers but the range is limited to 7 numbers. Hence the program repeats the number.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …