Write a program to find out the probability of each "total value" when several unbiased irregular dices (possibly with different number of faces) are thrown at the same time.
When an unbiased dice is thrown, the probability of having different face value should be equal. For instance, a typical cubic dice should give a probability of 1/6 for the face values 1,2,3,4,5 and 6. If two cubic dices were thrown, the total of the face values on the two dices is in range [2..12]. However, the probability of each "total value” is not equal. For example, the total of 4 is having a probability of 3/36 (for combinations 1+3, 2+2 and 3+3) while the probability of a total of 2 is only 1/36 (when both dices give 1).
Sample output as follow: (the one with * are the input from user)
Input the number of dice(s): *2
Input the number of faces for the 1st dice: *6
Input the number of faces for the 2nd dice: *6
Probability of 2 = 1/36
Probability of 3 = 2/36
Probability of 4 = 3/36
Probability of 5 = 4/36
Probability of 6 = 5/36
Probability of 7 = 6/36
Probability of 8 = 5/36
Probability of 9 = 4/36
Probability of 10 = 3/36
Probability of 11 = 2/36
Probability of 12 = 1/36
Input the number of dice(s): *5
Input the number of faces for the 1st dice: *1
Input the number of faces for the 2nd dice: *2
Input the number of faces for the 3rd dice: *3
Input the number of faces for the 4th dice: *4
Input the number of faces for the 5th dice: *5
Probability of 5 = 1/120
Probability of 6 = 4/120
Probability of 7 = 9/120
Probability of 8 = 15/120
Probability of 9 = 20/120
Probability of 10 = 22/120
Probability of 11 = 20/120
Probability of 12 = 15/120
Probability of 13 = 9/120
Probability of 14 = 4/120
Probability of 15 = 1/120
I don't actually know how to finish the probability part. I want to have some tips about the method to calculate the problem.
#include <iostream>
#include <string>
using namespace std;
//Initialise output function
string output(int num){
case 1:
return "st";
break;
case 2:
return "nd";
break;
case 3:
return "rd";
break;
default:
return "th";
}
//Roll function
int roll(int num, int result, int value[20]){
int dice[num][20];
for (int i=0; i<num;i++){
for (int j=1; j<=value[i];j++){
for (int k=0; k<value[i];k++)
dice[i][k]=j;
}
}
}
}
int main(){
int number;
//Initialise the number variable
cout <<"Input the number of dice(s): ";
cin >> number;
cout<<endl;
//Initialise the face of the dice using std::array
int value[11];
for (int i=0; i<number; i++){
cout << "Input the number of faces for the "<< i+1 << output(i+1)
<<" dice: ";
cin>>value[i];
}
//set the base of the probability (multiply), the maxrange (sum) for
the dice probability
int base=1;
int sum;
for (int i=0; i<number; i++){
base = base*value[i];
sum = sum +value[i];
}
//Output statements
if (sum >9){
for (int i=number; i<10; i++){
cout << "Probability of "<<i<<" = "<<roll(number, i, value);
}
for (int i=10; i<=sum;i++){
cout << "Probability of "<<i<<" = "<<roll(number, i, value);
}
} else {
for (int i=number; i<=sum; i++){
cout << "Probability of "<<i<<" = "<<roll(number, i, value);
}
}
return 0;
}
You can use brute froce and calculate all combinations using a recursive function and use a map to count the number each result occur.
Further, use the C++ containers instead of C-style arrays.
Like:
#include <iostream>
#include <vector>
#include <map>
void calcAll(const uint32_t value,
const uint32_t index,
const std::vector<uint32_t>& dices,
std::map<uint32_t, uint32_t>& count,
uint32_t& total)
{
if (index == dices.size())
{
// No more dices -> save result and stop recursion
auto it = count.find(value);
if (it == count.end())
{
count[value]=1;
}
else
{
count[value]++;
}
++total;
return;
}
// Iterate over all dice values
for (uint32_t i = 0; i < dices[index]; ++i)
{
calcAll(value + i + 1, index + 1, dices, count, total);
}
}
int main() {
std::vector<uint32_t> dices {6, 6, 6}; // 3 dices, 6 sides each
std::map<uint32_t, uint32_t> count;
uint32_t total = 0;
calcAll(0, 0, dices, count, total);
for (const auto& v : count)
{
std::cout << v.first << " seen " << v.second << " times out of " << total << std::endl;
}
return 0;
}
Output:
3 seen 1 times out of 216
4 seen 3 times out of 216
5 seen 6 times out of 216
6 seen 10 times out of 216
7 seen 15 times out of 216
8 seen 21 times out of 216
9 seen 25 times out of 216
10 seen 27 times out of 216
11 seen 27 times out of 216
12 seen 25 times out of 216
13 seen 21 times out of 216
14 seen 15 times out of 216
15 seen 10 times out of 216
16 seen 6 times out of 216
17 seen 3 times out of 216
18 seen 1 times out of 216