How to resolve the algorithm Dice game probabilities step by step in the C programming language
How to resolve the algorithm Dice game probabilities step by step in the C programming language
Table of Contents
Problem Statement
Two players have a set of dice each. The first player has nine dice with four faces each, with numbers one to four. The second player has six normal dice with six faces each, each face has the usual numbers from one to six. They roll their dice and sum the totals of the faces. The player with the highest total wins (it's a draw if the totals are the same). What's the probability of the first player beating the second player? Later the two players use a different set of dice each. Now the first player has five dice with ten faces each, and the second player has six dice with seven faces each. Now what's the probability of the first player beating the second player? This task was adapted from the Project Euler Problem n.205: https://projecteuler.net/problem=205
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dice game probabilities step by step in the C programming language
This C code calculates the probability of winning a dice game between two players with different numbers of dice and different sides on each die. Here's a breakdown of the code:
1. Definitions and Types:
typedef uint32_t uint;
: Defines a custom data typeuint
(unsigned 32-bit integer).typedef uint64_t ulong;
: Defines a custom data typeulong
(unsigned 64-bit integer).
2. Utility Functions:
ulong ipow(const uint x, const uint y);
: Raisesx
to the power ofy
using a loop.uint min(const uint x, const uint y);
: Returns the smaller of two unsigned integers.
3. Dice Rolling Function:
void throw_die(const uint n_sides, const uint n_dice, const uint s, uint counts[]);
: This function simulates rollingn_dice
dice withn_sides
sides. It computes the sums
of the rolled dice and updates thecounts
array to keep track of the frequency of each sum.
4. Probability Calculation Function:
double beating_probability(const uint n_sides1, const uint n_dice1, const uint n_sides2, const uint n_dice2);
: This is the main function that calculates the probability that Player 1 (withn_sides1
andn_dice1
) will beat Player 2 (withn_sides2
andn_dice2
). It does this by computing the probability of each possible outcome for both players and summing them up.
5. Main Function:
int main()
: This is the program's entry point. It calls thebeating_probability
function with specific parameters and prints the calculated probabilities for two different game scenarios.
Example Output:
0.4687500000000000
0.2343750000000000
These probabilities represent the chances of each player winning in the given game scenarios.
Source code in the c programming language
#include <stdio.h>
#include <stdint.h>
typedef uint32_t uint;
typedef uint64_t ulong;
ulong ipow(const uint x, const uint y) {
ulong result = 1;
for (uint i = 1; i <= y; i++)
result *= x;
return result;
}
uint min(const uint x, const uint y) {
return (x < y) ? x : y;
}
void throw_die(const uint n_sides, const uint n_dice, const uint s, uint counts[]) {
if (n_dice == 0) {
counts[s]++;
return;
}
for (uint i = 1; i < n_sides + 1; i++)
throw_die(n_sides, n_dice - 1, s + i, counts);
}
double beating_probability(const uint n_sides1, const uint n_dice1,
const uint n_sides2, const uint n_dice2) {
const uint len1 = (n_sides1 + 1) * n_dice1;
uint C1[len1];
for (uint i = 0; i < len1; i++)
C1[i] = 0;
throw_die(n_sides1, n_dice1, 0, C1);
const uint len2 = (n_sides2 + 1) * n_dice2;
uint C2[len2];
for (uint j = 0; j < len2; j++)
C2[j] = 0;
throw_die(n_sides2, n_dice2, 0, C2);
const double p12 = (double)(ipow(n_sides1, n_dice1) * ipow(n_sides2, n_dice2));
double tot = 0;
for (uint i = 0; i < len1; i++)
for (uint j = 0; j < min(i, len2); j++)
tot += (double)C1[i] * C2[j] / p12;
return tot;
}
int main() {
printf("%1.16f\n", beating_probability(4, 9, 6, 6));
printf("%1.16f\n", beating_probability(10, 5, 7, 6));
return 0;
}
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the ATS programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the ERRE programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the OCaml programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Jsish programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Phixmonti programming language