Program to generate a random number between L to R Last Updated : 18 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Write a program that generates a random number within a specified range [L, R]. The program should take two integers, L and R, as input and output a random number between L and R (inclusive). Examples: Input: L = 10, R = 20Output: 15 Input: L = -5, R = 5Output: 3 Approach: To solve the problem, follow the below idea: We can generate a random number in the range [L, R] by finding how many numbers are there between L to R by the formula: diff = (R - L + 1). Now, we can generate a random number and modulo the random number by diff and add the remainder to L to get a random number between L to R. Step-by-step algorithm: Find the difference between L to R by (R - L + 1).Now, generate a random number and modulo it by the above difference.Scale and shift the random number by adding the remainder to L to fit within the specified range [L, R].Below is the implementation of the algorithm: C++ #include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { int L = 10, R = 20; srand(time(NULL)); int random_number = L + rand() % (R - L + 1); cout << random_number << endl; return 0; } C #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int L = 10, R = 20; srand(time(NULL)); int random_number = L + rand() % (R - L + 1); printf("%d\n", random_number); return 0; } Java import java.util.Scanner; public class RandomNumberGenerator { public static void main(String[] args) { int L = 10; int R = 20; int random_number = L + (int) (Math.random() * (R - L + 1)); System.out.println(random_number); } } Python3 import random L, R = 10, 20 random_number = random.randint(L, R) print(random_number) C# using System; class Program { static void Main() { int L = 10; int R = 20; Random rand = new Random(); int random_number = rand.Next(L, R + 1); Console.WriteLine(random_number); } } JavaScript let L = 10 let R = 20 let random_number = Math.floor(Math.random() * (R - L + 1)) + L; console.log(random_number); Output15 Time Complexity: O(1), as it takes constant time to generate a random number.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to generate a random number between L to R S srinam Follow Improve Article Tags : DSA Similar Reads Program to generate a random two-digit number Write a program to generate a random two-digit number. Example 1: 73Example 2: 26 Approach: To solve the problem, follow the below idea: We can generate a random two-digit number by generating a random integer first and then modulo it by 90. Now, after modulo 90 the remainder can be in the range 0 t 2 min read Program to generate a random single digit number Write a program to generate a random single-digit number. Example 1: 7Example 2: 3 Approach: To solve the problem, follow the below idea: To generate a random single-digit number, we can first generate a random integer and then take apply modulo to get the last digit of that random integer. This las 2 min read How to generate a random number between 0 and 1 in Python ? The use of randomness is an important part of the configuration and evaluation of modern algorithms. Here, we will see the various approaches for generating random numbers between 0 ans 1. Method 1: Here, we will use uniform() method which returns the random number between the two specified numbers 1 min read R Program to Generate a Random Password Password generation is a common task in programming languages. It is required for security applications and various accounts managing systems. A random password is not easily guessable which also improves the security of the accounts systems with the aim to protect information. In R Programming Lang 4 min read Generate a Random Number between 0 and 1 The Task is to generate a random number between 0 and 1. It is obvious that the number between 0 and 1 will be a floating point number. To generate a random number between 0 and 1, we will make use of the rand() function. The rand() function creates a random number. Approach: Generating a Random Num 5 min read Like