86.2k views
4 votes
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++?

User Broesch
by
7.2k points

1 Answer

2 votes

Answer:

Step-by-step explanation:

Here's an example code in C++ that generates a winning lottery ticket consisting of three random numbers in the range of smallestVal to greatestVal:

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

int seedVal, smallestVal, greatestVal;

cout << "Enter seed value: ";

cin >> seedVal;

cout << "Enter smallest value: ";

cin >> smallestVal;

cout << "Enter greatest value: ";

cin >> greatestVal;

// Seed the random number generator with the user-input seed value

srand(seedVal);

// Generate three random numbers in the range of smallestVal to greatestVal

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

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

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

// Output the winning lottery ticket

cout << lottery1 << endl;

cout << lottery2 << endl;

cout << lottery3 << endl;

return 0;

}

This code prompts the user to enter a seed value, the smallest and greatest values for the range of the lottery numbers. It then seeds the random number generator with the user-input seed value, generates three random numbers using rand() % (greatestVal - smallestVal + 1) + smallestVal to ensure the numbers fall within the range of smallestVal to greatestVal, and outputs the results on separate lines with a newline character.

User Amir Khan
by
7.5k points