Guest User

Untitled

a guest
May 4th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. public class Program
  7. {
  8.     static Random rng = new Random();
  9.  
  10.     public static void Main()
  11.     {
  12.        
  13.         var parseResult = ParseLogFile(@"C:\temp\chat.log");
  14.         var observedFailureChanceDaoc = OutputResults("Original", parseResult);
  15.  
  16.         var randomResult = ParseRandomFile(@"c:\temp\random.txt", observedFailureChanceDaoc);
  17.         OutputResults("Random.Org", randomResult, observedFailureChanceDaoc);
  18.  
  19.         var csharpRandom = GenerateTestDataSet(parseResult.Count, observedFailureChanceDaoc);
  20.         OutputResults("C# System.Random", csharpRandom, observedFailureChanceDaoc);
  21.  
  22.         var csharpRandomLarge = GenerateTestDataSet(20000, observedFailureChanceDaoc);
  23.         OutputResults("C# System.Random Large Data Set", csharpRandomLarge, observedFailureChanceDaoc);
  24.  
  25.         Console.ReadKey();
  26.     }
  27.  
  28.     public static decimal OutputResults(string title, List<bool> resultSet, decimal targetFailureChance = 0, bool verbose = false)
  29.     {
  30.         Console.WriteLine("-----------------------------------------------------------------");
  31.         Console.WriteLine($"{ title }");
  32.         Console.WriteLine("-----------------------------------------------------------------");
  33.         var totalFailures = resultSet.Count(chance => chance == false);
  34.         var failureChance = targetFailureChance > 0 ? targetFailureChance : totalFailures / (decimal)resultSet.Count;
  35.         Console.WriteLine($"Total Attempts: { resultSet.Count }");
  36.         Console.WriteLine($"Total Failures: { totalFailures }");
  37.         Console.WriteLine($"Observed Failure Chance: { string.Format("Value: {0:P2}.", totalFailures / (decimal)resultSet.Count) }");
  38.         var streaks = GetStreaks(resultSet, 1);
  39.         OutputStreakBreakdown(streaks, failureChance);
  40.         if (verbose)
  41.         {
  42.             OutputStreaks(streaks, failureChance);
  43.         }
  44.         return failureChance;
  45.     }
  46.  
  47.     public static List<bool> ParseRandomFile(string filePath, decimal failureChance)
  48.     {
  49.         var check = failureChance * 100;
  50.         var lines = File.ReadAllLines(filePath);
  51.         var results = new List<bool>();
  52.         foreach(var line in lines)
  53.         {
  54.             var number = Int16.Parse(line);
  55.             results.Add(check <= number);
  56.         }
  57.         return results;
  58.     }
  59.     public static List<bool> ParseLogFile(string filePath)
  60.     {
  61.         var log = File.ReadAllLines(filePath);
  62.         var results = new List<bool>();
  63.  
  64.         var currentlyCasting = false;
  65.         foreach (var line in log)
  66.         {
  67.             if (line.EndsWith("You begin casting a Induce Disease spell!"))
  68.             {
  69.                 currentlyCasting = true;
  70.             }
  71.             else if (currentlyCasting)
  72.             {
  73.                 if (line.EndsWith("Awoga resists the effect!"))
  74.                 {
  75.                     currentlyCasting = false;
  76.                     results.Add(false);
  77.                 }
  78.                 else if (line.EndsWith("Awoga already has that effect. ") || line.EndsWith("Awoga is diseased!"))
  79.                 {
  80.                     currentlyCasting = false;
  81.                     results.Add(true);
  82.                    
  83.                 }
  84.                 else if (line.Contains("You begin casting") || line.Contains("You sit down."))
  85.                 {
  86.                     //not casting disease so ignore it.
  87.                     currentlyCasting = false;
  88.                 }
  89.             }
  90.         }
  91.         return results;
  92.     }
  93.     public static List<bool> GenerateTestDataSet(int size, decimal failureChance) => Enumerable.Range(0, size).Select(r => Chance(failureChance)).ToList();
  94.     public static List<Streak> GetStreaks(IEnumerable<bool> dataSet, int minimumLength, bool showSuccess = false)
  95.     {
  96.         bool lastResult = false;
  97.         int currentStreakLength = 0;
  98.  
  99.         var results = new List<Streak>();
  100.  
  101.         var setSize = dataSet.Count();
  102.         for (var i = 0; i < setSize; i++)
  103.         {
  104.             var cast = dataSet.ElementAt(i);
  105.             if (cast == lastResult)
  106.             {
  107.                 currentStreakLength++;
  108.                 if (i == setSize - 1 && currentStreakLength >= minimumLength && (showSuccess || !lastResult))
  109.                 {
  110.                     var streakId = results.Count() + 1;
  111.                     results.Add(new Streak() { Length = currentStreakLength, Type = lastResult });
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 if (currentStreakLength >= minimumLength && (showSuccess || !lastResult))
  117.                 {
  118.                     var streakId = results.Count() + 1;
  119.                     results.Add(new Streak() { Length = currentStreakLength, Type = lastResult });
  120.                 }
  121.                 currentStreakLength = 1;
  122.                 lastResult = cast;
  123.             }
  124.         }
  125.  
  126.         return results;
  127.     }
  128.     public static void OutputStreaks(List<Streak> streaks, decimal failureChance = 0, int minimumLength = 2)
  129.     {
  130.         Console.ForegroundColor = ConsoleColor.White;
  131.         foreach (var streak in streaks.Where(a => a.Length >= minimumLength).OrderByDescending(a => a.Length))
  132.         {
  133.             var type = streak.Type ? "Success" : "Failure";
  134.  
  135.             if (streak.Type == false)
  136.             {
  137.                 Console.ForegroundColor = ConsoleColor.Gray;
  138.             }
  139.             else
  140.             {
  141.                 Console.ForegroundColor = ConsoleColor.Green;
  142.             }
  143.  
  144.             var textOut = $"{ type } - streak of { streak.Length }";
  145.  
  146.             if (failureChance > 0)
  147.             {
  148.                 var adjustedChance = streak.Type == true ? 100 - failureChance : failureChance;
  149.                 var probability = ProbabilityCalculator(adjustedChance, streak.Length);
  150.                 var chanceIn = 1 / probability;
  151.                 textOut += $" - probability: one in  { string.Format("{0:n0}", chanceIn) }";
  152.             }
  153.  
  154.             Console.WriteLine(textOut);
  155.         }
  156.         Console.ForegroundColor = ConsoleColor.Gray;
  157.     }
  158.     public static void OutputStreakBreakdown(List<Streak> streaks, decimal failureChance = 0, int minimumLength = 2)
  159.     {
  160.         Console.ForegroundColor = ConsoleColor.Green;
  161.         foreach (var streakSet in streaks
  162.             .Where(a => a.Length >= minimumLength)
  163.             .GroupBy(a => $"Streaks of { a.Length } { (a.Type ? "Successes" : "Failures") } ")
  164.             .OrderByDescending(a => a.First().Length))
  165.         {
  166.             var textOut = $"{ streakSet.Key } - seen { streakSet.Count() } time(s)";
  167.             var first = streakSet.First();
  168.             var adjustedChance = first.Type == true ? 100 - failureChance : failureChance;
  169.             var probability = ProbabilityCalculator(adjustedChance, first.Length);
  170.             var chanceIn = 1 / probability;
  171.             textOut += $" - probability: one in  { string.Format("{0:n0}", chanceIn) } @ { string.Format("Value: {0:P2}.", failureChance) }";
  172.             Console.WriteLine(textOut);
  173.         }
  174.         Console.ForegroundColor = ConsoleColor.Gray;
  175.     }
  176.     public static decimal ProbabilityCalculator(decimal probability, int numberTimes)
  177.     {
  178.         return (decimal)Math.Pow((double)probability, numberTimes);
  179.     }
  180.     public static bool Chance(decimal chance)
  181.     {
  182.         return (chance * 100) < Random(0, 100);
  183.     }
  184.     public static int Random(int min, int max)
  185.     {
  186.         return rng.Next(min, max + 1);
  187.     }
  188. }
  189.  
  190.  
  191. public class Streak
  192. {
  193.     public bool Type { get; set; }
  194.     public int Length { get; set; }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment