160k views
1 vote
PLEASE HELP!

Given integer variables seedVal and sidesVal, output two random dice rolls. The die is numbered from 1 to sidesVal. End each output with a newline.

Ex: If sidesVal is 6, then one possible output is:

1
5
how would I code this in c++?

User TheOne
by
9.2k points

1 Answer

2 votes

Answer:

Step-by-step explanation:

Here's an example code in C++ that generates two random dice rolls based on the input sidesVal:

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

int seedVal, sidesVal;

cout << "Enter seed value: ";

cin >> seedVal;

cout << "Enter number of sides: ";

cin >> sidesVal;

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

srand(seedVal);

// Generate two random numbers in the range of 1 to sidesVal

int roll1 = rand() % sidesVal + 1;

int roll2 = rand() % sidesVal + 1;

// Output the results

cout << roll1 << endl;

cout << roll2 << endl;

return 0;

}

This code prompts the user to enter a seed value and the number of sides on the die. It then seeds the random number generator with the user-input seed value, generates two random numbers using rand() % sidesVal + 1 to ensure the numbers fall within the range of 1 to sidesVal, and outputs the results on separate lines with a newline character.

User Pull
by
7.9k points