- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Generate Randomized Sequence of Given Range of Numbers
At first let us discuss about the rand() function. rand() function is a predefined method of C++. It is declared in <stdlib.h> header file. rand() is used to generate random number within a range. Here min_n is the minimum range of the random numbers and max_n is the maximum range of the numbers. So rand() will return the random numbers between min_n to (max_n – 1) inclusive of the limit values. Here if we mention lower and upper limits as 1 and 100 respectively, then rand() will return values from 1 to (100 – 1). i.e. from 1 to 99.
Algorithm
Begin Declare max_n to the integer datatype. Initialize max_n = 100. Declare min_n to the integer datatype. Initialize min_n = 1. Declare new_n to the integer datatype. Declare i of integer datatype. Print “The random number is:”. for (i = 0; i < 10; i++) new_n = ((rand() % (max_n + 1 - min_n)) + min_n) Print the value of new_n. End.
Example
#include <iostream> #include <stdlib.h> using namespace std; int main() { int max_n = 100; int min_n = 1; int new_n; int i; cout<<"The random number is: \n"; for (i = 0; i < 10; i++) { new_n = ((rand() % (max_n + 1 - min_n)) + min_n); //rand() returns random decimal number. cout<<new_n<<endl; } return 0; }
Output
The random number is: 42 68 35 1 70 25 79 59 63 65
- Related Articles
- C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range
- C++ Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range
- C++ Program to Implement Segmented Sieve to Generate Prime Numbers Between Given Range
- C++ Program to Generate a Sequence of N Characters for a Given Specific Case
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- C++ Program to Generate a Graph for a Given Fixed Degree Sequence
- Program to find bitwise AND of range of numbers in given range in Python
- Python program to generate random numbers within a given range and store in a list?
- Java program to generate random numbers within a given range and store in a list
- C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters
- Program to find sum of given sequence in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- 8085 program to generate Fibonacci sequence

Advertisements