Dice rolls: Counting number of rolls that equals 6 or 7

  • Context: MHB 
  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    Counting Dice
Click For Summary
SUMMARY

This discussion focuses on a C++ program that calculates the number of times the sum of two randomly rolled dice equals six or seven. The program utilizes the rand() function for generating random numbers and employs a loop to iterate through user-defined rolls. Key enhancements suggested include expanding the program to count occurrences for all possible sums from 2 to 12, implementing a while loop for continuous user input until a valid number of rolls is provided, and creating a histogram to visually represent the frequency of each sum.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of random number generation using rand()
  • Basic knowledge of loops and conditionals in C++
  • Familiarity with console input and output in C++
NEXT STEPS
  • Implement a C++ program to count occurrences of dice sums from 2 to 12
  • Modify the existing program to use a while loop for continuous input
  • Create a histogram output in C++ using console characters
  • Explore advanced random number generation techniques in C++
USEFUL FOR

C++ developers, educators teaching programming concepts, and anyone interested in statistical simulations using random number generation.

carl123
Messages
55
Reaction score
0
The following calculates the number of times the sum of two dice (randomly rolled) equals six or seven.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(){ 
int i = 0; // Loop counter iterates numRolls times
int numRolls = 0; // User defined number of rolls
int numSixes = 0; // Tracks number of 6s found
int numSevens = 0; // Tracks number of 7s found
int die1 = 0; // Dice values
int die2 = 0; // Dice values
int rollTotal = 0; // Sum of dice values

cout << "Enter number of rolls: " << endl;
cin >> numRolls;

srand(time(0));

if (numRolls >= 1) {
// Roll dice numRoll times
for (i = 0; i < numRolls; ++i) {
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
rollTotal = die1 + die2;

// Count number of sixs and sevens
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
else if (rollTotal == 7) {
numSevens = numSevens + 1;
}
cout << endl << "Roll " << (i + 1) << " is "
<< rollTotal << " (" << die1
<< "+" << die2 << ")";
}

// Print statistics on dice rolls
cout << endl << endl;
cout << "Dice roll statistics:" << endl;
cout << "6s: " << numSixes << endl;
cout << "7s: " << numSevens << endl;
}
else {
cout << "Invalid rolls. Try again." << endl;
}

return 0;
}

QUESTION

Create different versions of the program that:

1) Calculates the number of times the sum of the randomly rolled dice equals each possible value from 2 to 12.

2) Repeatedly asks the user for the number of times to roll the dice, quitting only when the user-entered number is less than 1. Hint: Use a while loop that will execute as long as numRolls is greater than 1. Be sure to initialize numRolls correctly.

3) Prints a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times, as shown below.

Dice roll histogram:

2: ******
3: ****
4: ***
5: ********
6: *******************
7: *************
8: *************
9: **************
10: ***********
11: *****
12: ****
 
Technology news on Phys.org
Can you show what you'e tried? It's hard to know how to help you when you merely post problems with no effort shown. Our goal is to guide, not do your work. Please add your work to each thread you've posted where you have not already done so, and you will find people will be much more likely to help. :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
Replies
17
Views
10K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K