0% found this document useful (0 votes)
4 views2 pages

C++ Random Number Generator Example

The document presents a C++ solution for generating random numbers within a specified range. It includes a function that generates a random number between two values and demonstrates its usage in the main function. The random number generator is seeded to ensure different outputs each time the program runs.

Uploaded by

ziadkaoubi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

C++ Random Number Generator Example

The document presents a C++ solution for generating random numbers within a specified range. It includes a function that generates a random number between two values and demonstrates its usage in the main function. The random number generator is seeded to ensure different outputs each time the program runs.

Uploaded by

ziadkaoubi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem # 19/2 Solution Using C++

#include <iostream>
#include <cstdlib>

using namespace std;

int RandomNumber(int From, int To)


{
//Function to generate a random number

int randNum = rand() % (To - From + 1) + From;


return randNum;
}

int main() {
//Seeds the random number generator in C++, called only once
srand((unsigned)time(NULL));

cout << RandomNumber(1, 10) << endl;


cout << RandomNumber(1, 10) << endl;
cout << RandomNumber(1, 10) << endl;

return 0;

[Link]
© Copyright 2022

You might also like