Mathieu GAILLARD

Finding pairs of dices that add up to 7

In my Algorithm class at Purdue University, I got an interesting probability question, and I brute forced it in C++ to check my answer.

Suppose we roll 5 dices, what is the expected number of pairs of dices that add up to 7?

Example: if we roll the dices and get: 1, 6, 5, 2, 6; we got 3 good pairs.

Here is the C++ code that gives the statistics:

#include <iostream>
#include <array>

using namespace std;

template<unsigned int N>
int compute_nb_good_pairs(const array<int, N>& dices)
{
    int nb_good_pairs = 0;

    for (int i = 0; i < dices.size(); i++)
    {
        for (int j = i + 1; j < dices.size(); j++)
        {
            if (dices[i] + dices[j] == 7)
            {
                nb_good_pairs++;
            }
        }
    }

    return nb_good_pairs;
}

int main()
{
    array<int, 11> histogram = {0};

    int total_nb_good_pairs = 0;
    int total_nb_arrays = 0;

    // Enumerate all possible outcomes with 5 dices
    for (int a = 1; a <= 6; a++) {
        for (int b = 1; b <= 6; b++) {
            for (int c = 1; c <= 6; c++) {
                for (int d = 1; d <= 6; d++) {
                    for (int e = 1; e <= 6; e++) {
                        const auto nb_good_pairs = compute_nb_good_pairs<5>({{a, b, c, d, e}});

                        histogram[nb_good_pairs]++;
                        total_nb_good_pairs += nb_good_pairs;
                        total_nb_arrays++;
                    }
                }
            }
        }
    }

    // Display some interesting stats
    cout << "Histogram:" << endl;
    for (int i = 0; i < histogram.size(); i++)
    {
        cout << i << " => " << histogram[i] << endl;
    }

    cout << "Total number of tests: " << total_nb_arrays << endl;
    cout << "Total number of good pairs: " << total_nb_good_pairs << endl;
    cout << "Expected number of good pairs: " << double(total_nb_good_pairs) / total_nb_arrays << endl;

    return 0;
}

Output:

Histogram:
0 => 1566
1 => 1680
2 => 2880
3 => 1200
4 => 390
5 => 0
6 => 60
7 => 0
8 => 0
9 => 0
10 => 0
Total number of tests: 7776
Total number of good pairs: 12960
Expected number of good pairs: 1.66667

The answer is 1.6667, which is equal to 5/3. Why? Probably because in a set of 5 dices, it’s possible to make 10 different pairs, and the probability of a pair of dices to add up to 7 is 1/6. So, 10/6 gives us the answer 5/3. Interestingly, it’s not possible to have exactly 5 pairs, I don’t know why, but it could be cool to investigate…