C++ program to take a max limit from user and generate a random number till that number

Views: 100   

Following program shows you how to take a max limit from user and generate a random number till that number.
In this program we get max limit number from user and generate a random number using rand()

#include <iostream>
int main() {
    int maxLimit;
    int lower = 0;
    std::cout << "Enter maxlimit to generate a random number:";
    std::cin >> maxLimit;
    int result = (rand() + lower) % (maxLimit + 1);
    std::cout << "Random number is:" << result;
}

Output:

Example1:

Enter maxlimit to generate a random number: 100
Random number is:32

Example2:

Enter maxlimit to generate a random number: 1500
Random number is:325
On By