129 lines
No EOL
5.2 KiB
C#
129 lines
No EOL
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace DiceProbabilities
|
|
{
|
|
public static class RollFourSumTwo
|
|
{
|
|
public static readonly Dictionary<string, float> ResultOddsmapping = new Dictionary<string, float>()
|
|
{
|
|
{"=", .6296f },
|
|
{"2", .1319f},
|
|
{"3", .2330f},
|
|
{"4", .3557f},
|
|
{"5", .4475f},
|
|
{"6", .5610f},
|
|
{"7", .6435f},
|
|
{"8", .5610f},
|
|
{"9", .4475f},
|
|
{"10", .3557f},
|
|
{"11", .2340f},
|
|
{"12", .1319f}
|
|
|
|
};
|
|
|
|
public static float GetProbalityOfAtleastOneUnlocking(List<string> options)
|
|
{
|
|
float probabilityNoDiceWillSumToLocks = 1;
|
|
foreach (var option in options)
|
|
{
|
|
if (ResultOddsmapping.TryGetValue(option, out var value))
|
|
{
|
|
probabilityNoDiceWillSumToLocks *= (1 - value);
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException($"{option} is not a valid result for the sum of two dice.");
|
|
}
|
|
}
|
|
|
|
return 1 - probabilityNoDiceWillSumToLocks;
|
|
}
|
|
|
|
public static string RollFourPickTwoPickTwoAgainOdds()
|
|
{
|
|
// Total number of rolls (6^4 = 1296 possible outcomes)
|
|
int possibleDiceCombosCount = 0;
|
|
|
|
// Counter for the possible sums
|
|
Dictionary<(string, string), int> selectionsCounts = new Dictionary<(string, string), int>();
|
|
double diceEqualCount = 0;
|
|
|
|
// Generate all possible rolls of 4 dice
|
|
for (int d1 = 1; d1 <= 6; d1++)
|
|
{
|
|
for (int d2 = 1; d2 <= 6; d2++)
|
|
{
|
|
for (int d3 = 1; d3 <= 6; d3++)
|
|
{
|
|
for (int d4 = 1; d4 <= 6; d4++)
|
|
{
|
|
// Get all possible pairs of dice
|
|
var possibleDiceSlections = new List<((int, int), (int, int))>
|
|
{
|
|
((d1, d2), (d3, d4)), ((d1, d3), (d2, d4)), ((d1, d4), (d2, d3)),
|
|
((d2, d3), (d1, d4)), ((d2, d4), (d1, d3)),
|
|
((d3, d4), (d1, d2))
|
|
};
|
|
|
|
foreach (((int, int), (int, int)) selections in possibleDiceSlections)
|
|
{
|
|
possibleDiceCombosCount++;
|
|
var firstSelection = selections.Item1;
|
|
var secondSelection = selections.Item2;
|
|
string firstSelectionResult;
|
|
string secondSelectionResult;
|
|
if (firstSelection.Item1 == firstSelection.Item2)
|
|
{
|
|
firstSelectionResult = "=";
|
|
if (secondSelection.Item1 == secondSelection.Item2)
|
|
{
|
|
secondSelectionResult = "=";
|
|
IncrementSelectionsCounts(selectionsCounts, (firstSelectionResult, secondSelectionResult));
|
|
}
|
|
|
|
secondSelectionResult = (secondSelection.Item1 + secondSelection.Item2).ToString();
|
|
IncrementSelectionsCounts(selectionsCounts, (firstSelectionResult, secondSelectionResult));
|
|
}
|
|
|
|
firstSelectionResult = (firstSelection.Item1 + firstSelection.Item2).ToString();
|
|
if (secondSelection.Item1 == secondSelection.Item2)
|
|
{
|
|
secondSelectionResult = "=";
|
|
IncrementSelectionsCounts(selectionsCounts, (firstSelectionResult, secondSelectionResult));
|
|
}
|
|
|
|
secondSelectionResult = (secondSelection.Item1 + secondSelection.Item2).ToString();
|
|
IncrementSelectionsCounts(selectionsCounts, (firstSelectionResult, secondSelectionResult));
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("Sum\tProbability (%)" + Environment.NewLine);
|
|
|
|
// sb.Append($"=\t{diceEqualCount / possibleDiceCombosCount * 100:F2}%{Environment.NewLine}");
|
|
foreach (var selections in selectionsCounts.Keys)
|
|
{
|
|
double probability = (double)selectionsCounts[selections] / possibleDiceCombosCount * 100;
|
|
sb.Append($"{selections}\t{probability:F2}%{Environment.NewLine}");
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static void IncrementSelectionsCounts(Dictionary<(string, string), int> dict, (string, string) key)
|
|
{
|
|
if (!dict.ContainsKey(key))
|
|
{
|
|
dict[key] = 0;
|
|
}
|
|
|
|
dict[key]++;
|
|
}
|
|
}
|
|
} |