84.5k views
1 vote
Given integer variables seedVal, smallestVal, and greatestVal, output a winning lottery ticket consisting of three random numbers in the range of smallestVal to greatestVal inclusive. End each output with a newline.

Ex: If smallestVal is 30 and greatestVal is 80, then one possible output is:

65
61
41

how do I code this in c++?

1 Answer

3 votes

Answer:

Step-by-step explanation:

Here's an example code in C++ that generates three random numbers within the range of smallestVal and greatestVal inclusive, using seedVal as the random seed:

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

int seedVal, smallestVal, greatestVal;

// get input values for seedVal, smallestVal, greatestVal

// set the random seed

srand(seedVal);

// generate three random numbers and output them

for (int i = 0; i < 3; i++) {

int randNum = rand() % (greatestVal - smallestVal + 1) + smallestVal;

cout << randNum << endl;

}

return 0;

}

In this code, the srand() function is used to set the random seed to seedVal, so that each time the program is run with the same seedVal, the same set of random numbers will be generated. The rand() function is then used to generate a random number within the range of smallestVal and greatestVal, and the result is output to the console using cout. The loop is used to generate three random numbers.

User Nouney
by
8.0k points