Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- public class Program
- {
- static Random rng = new Random();
- public static void Main()
- {
- var parseResult = ParseLogFile(@"C:\temp\chat.log");
- var observedFailureChanceDaoc = OutputResults("Original", parseResult);
- var randomResult = ParseRandomFile(@"c:\temp\random.txt", observedFailureChanceDaoc);
- OutputResults("Random.Org", randomResult, observedFailureChanceDaoc);
- var csharpRandom = GenerateTestDataSet(parseResult.Count, observedFailureChanceDaoc);
- OutputResults("C# System.Random", csharpRandom, observedFailureChanceDaoc);
- var csharpRandomLarge = GenerateTestDataSet(20000, observedFailureChanceDaoc);
- OutputResults("C# System.Random Large Data Set", csharpRandomLarge, observedFailureChanceDaoc);
- Console.ReadKey();
- }
- public static decimal OutputResults(string title, List<bool> resultSet, decimal targetFailureChance = 0, bool verbose = false)
- {
- Console.WriteLine("-----------------------------------------------------------------");
- Console.WriteLine($"{ title }");
- Console.WriteLine("-----------------------------------------------------------------");
- var totalFailures = resultSet.Count(chance => chance == false);
- var failureChance = targetFailureChance > 0 ? targetFailureChance : totalFailures / (decimal)resultSet.Count;
- Console.WriteLine($"Total Attempts: { resultSet.Count }");
- Console.WriteLine($"Total Failures: { totalFailures }");
- Console.WriteLine($"Observed Failure Chance: { string.Format("Value: {0:P2}.", totalFailures / (decimal)resultSet.Count) }");
- var streaks = GetStreaks(resultSet, 1);
- OutputStreakBreakdown(streaks, failureChance);
- if (verbose)
- {
- OutputStreaks(streaks, failureChance);
- }
- return failureChance;
- }
- public static List<bool> ParseRandomFile(string filePath, decimal failureChance)
- {
- var check = failureChance * 100;
- var lines = File.ReadAllLines(filePath);
- var results = new List<bool>();
- foreach(var line in lines)
- {
- var number = Int16.Parse(line);
- results.Add(check <= number);
- }
- return results;
- }
- public static List<bool> ParseLogFile(string filePath)
- {
- var log = File.ReadAllLines(filePath);
- var results = new List<bool>();
- var currentlyCasting = false;
- foreach (var line in log)
- {
- if (line.EndsWith("You begin casting a Induce Disease spell!"))
- {
- currentlyCasting = true;
- }
- else if (currentlyCasting)
- {
- if (line.EndsWith("Awoga resists the effect!"))
- {
- currentlyCasting = false;
- results.Add(false);
- }
- else if (line.EndsWith("Awoga already has that effect. ") || line.EndsWith("Awoga is diseased!"))
- {
- currentlyCasting = false;
- results.Add(true);
- }
- else if (line.Contains("You begin casting") || line.Contains("You sit down."))
- {
- //not casting disease so ignore it.
- currentlyCasting = false;
- }
- }
- }
- return results;
- }
- public static List<bool> GenerateTestDataSet(int size, decimal failureChance) => Enumerable.Range(0, size).Select(r => Chance(failureChance)).ToList();
- public static List<Streak> GetStreaks(IEnumerable<bool> dataSet, int minimumLength, bool showSuccess = false)
- {
- bool lastResult = false;
- int currentStreakLength = 0;
- var results = new List<Streak>();
- var setSize = dataSet.Count();
- for (var i = 0; i < setSize; i++)
- {
- var cast = dataSet.ElementAt(i);
- if (cast == lastResult)
- {
- currentStreakLength++;
- if (i == setSize - 1 && currentStreakLength >= minimumLength && (showSuccess || !lastResult))
- {
- var streakId = results.Count() + 1;
- results.Add(new Streak() { Length = currentStreakLength, Type = lastResult });
- }
- }
- else
- {
- if (currentStreakLength >= minimumLength && (showSuccess || !lastResult))
- {
- var streakId = results.Count() + 1;
- results.Add(new Streak() { Length = currentStreakLength, Type = lastResult });
- }
- currentStreakLength = 1;
- lastResult = cast;
- }
- }
- return results;
- }
- public static void OutputStreaks(List<Streak> streaks, decimal failureChance = 0, int minimumLength = 2)
- {
- Console.ForegroundColor = ConsoleColor.White;
- foreach (var streak in streaks.Where(a => a.Length >= minimumLength).OrderByDescending(a => a.Length))
- {
- var type = streak.Type ? "Success" : "Failure";
- if (streak.Type == false)
- {
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Green;
- }
- var textOut = $"{ type } - streak of { streak.Length }";
- if (failureChance > 0)
- {
- var adjustedChance = streak.Type == true ? 100 - failureChance : failureChance;
- var probability = ProbabilityCalculator(adjustedChance, streak.Length);
- var chanceIn = 1 / probability;
- textOut += $" - probability: one in { string.Format("{0:n0}", chanceIn) }";
- }
- Console.WriteLine(textOut);
- }
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- public static void OutputStreakBreakdown(List<Streak> streaks, decimal failureChance = 0, int minimumLength = 2)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- foreach (var streakSet in streaks
- .Where(a => a.Length >= minimumLength)
- .GroupBy(a => $"Streaks of { a.Length } { (a.Type ? "Successes" : "Failures") } ")
- .OrderByDescending(a => a.First().Length))
- {
- var textOut = $"{ streakSet.Key } - seen { streakSet.Count() } time(s)";
- var first = streakSet.First();
- var adjustedChance = first.Type == true ? 100 - failureChance : failureChance;
- var probability = ProbabilityCalculator(adjustedChance, first.Length);
- var chanceIn = 1 / probability;
- textOut += $" - probability: one in { string.Format("{0:n0}", chanceIn) } @ { string.Format("Value: {0:P2}.", failureChance) }";
- Console.WriteLine(textOut);
- }
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- public static decimal ProbabilityCalculator(decimal probability, int numberTimes)
- {
- return (decimal)Math.Pow((double)probability, numberTimes);
- }
- public static bool Chance(decimal chance)
- {
- return (chance * 100) < Random(0, 100);
- }
- public static int Random(int min, int max)
- {
- return rng.Next(min, max + 1);
- }
- }
- public class Streak
- {
- public bool Type { get; set; }
- public int Length { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment