Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace SpellProbability
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- //SpellbookToIndependent();
- IndependentToSpellbook();
- }
- /// <summary>
- /// Converts a monster spellbook to a list of independent probabilities
- /// </summary>
- public static void SpellbookToIndependent()
- {
- var chances = new List<float>() { 0.5f, 0.5f };
- Console.WriteLine($"Spellbook probabilities: {string.Join(", ", chances.Select(i => PercentFormat(i)))}\n");
- var castChance = GetProbabilityAny(chances);
- Console.WriteLine($"Total casting chance: {PercentFormat(castChance)}\n");
- for (var i = 0; i < chances.Count; i++)
- {
- var prevChanceNone = i > 0 ? GetProbabilityNone(chances.GetRange(0, i)) : 1.0f;
- Console.WriteLine($"Chance of casting spell #{i + 1}: {PercentFormat(chances[i] * prevChanceNone)}");
- }
- }
- /// <summary>
- /// Converts a list of independent probabilities to monster spellbook format
- /// </summary>
- public static void IndependentToSpellbook()
- {
- var chances = new List<float>() { 0.25f, 0.25f, 0.25f, 0.25f };
- Console.WriteLine($"Independent probabilities (cannot go above 100% additive): {string.Join(", ", chances.Select(i => PercentFormat(i)))}\n");
- var spellbook = new List<float>();
- for (var i = 0; i < chances.Count; i++)
- {
- var prevChanceNone = i > 0 ? GetProbabilityNone(spellbook) : 1.0f;
- spellbook.Add(chances[i] / prevChanceNone);
- }
- Console.WriteLine($"Spellbook format: {string.Join(", ", spellbook.Select(i => PercentFormat(i)))}\n");
- }
- /// <summary>
- /// Returns the probability of none of the events occurring
- /// from a list of chances
- /// </summary>
- public static float GetProbabilityNone(List<float> chances)
- {
- var probability = 1.0f;
- foreach (var chance in chances)
- probability *= 1.0f - chance;
- return probability;
- }
- /// <summary>
- /// Returns the probability of any of the events occurring
- /// from a list of chances
- /// </summary>
- public static float GetProbabilityAny(List<float> chances)
- {
- return 1.0f - GetProbabilityNone(chances);
- }
- public static string PercentFormat(float percent)
- {
- return $"{Math.Round(percent * 100, 2)}%";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment