Skip to content

Instantly share code, notes, and snippets.

@SteffenBlake
Created March 27, 2021 00:20
Show Gist options
  • Save SteffenBlake/574987a98e35e7fe9204a277ef788db8 to your computer and use it in GitHub Desktop.
Save SteffenBlake/574987a98e35e7fe9204a277ef788db8 to your computer and use it in GitHub Desktop.
Simple example program for comparing seed rolling algs
using System;
using System.Collections.Generic;
namespace ConsoleApp3
{
class Program
{
const double Thresh1 = 0.6;
const double Thresh2 = 0.7;
const double Thresh3 = 0.8;
private const int Rolls = 2;
private const int Trials = 100000;
static void Main(string[] args)
{
Console.WriteLine($"Threshold 1: {Thresh1:P0}");
Console.WriteLine($"Threshold 2: {Thresh2:P0}");
Console.WriteLine($"Threshold 3: {Thresh3:P0}");
Console.WriteLine($"Trials: {Trials}");
Console.WriteLine();
var rnd = new Random();
RunTrials(rnd, Trials, 1, 1);
RunTrials(rnd, Trials, Rolls, 1);
RunTrials(rnd, Trials, 1, Rolls);
Console.WriteLine("Press enter to end the program");
Console.ReadKey();
}
static void RunTrials(Random rnd, int trials, int innerRolls, int outerRolls)
{
var average = 0.0;
var results = new Dictionary<double, int>
{
{1, 0},{2, 0},{3, 0},{4, 0},
};
for (var n = 1; n <= trials; n++)
{
var attempt = AttemptSeed(rnd, innerRolls, outerRolls);
average += attempt / trials;
results[attempt]++;
}
Console.WriteLine($"Outer Rolls: {outerRolls}");
Console.WriteLine($"Inner Rolls: {innerRolls}");
Console.WriteLine($"Average Result: {average}");
foreach (var result in results)
{
Console.WriteLine($"{result.Key}: {result.Value}");
}
Console.WriteLine();
}
static double AttemptSeed(Random rnd, int innerRolls, int outerRolls)
{
var lv = 1.0;
for (var n = 1; n <= outerRolls; n++)
lv = Math.Max(lv, RollSeed(rnd, innerRolls));
return lv;
}
static double RollSeed(Random rnd, int rolls)
{
if (RollChance(rnd, rolls) < Thresh1)
return 1;
if (RollChance(rnd, rolls) < Thresh2)
return 2;
if (RollChance(rnd, rolls) < Thresh3)
return 3;
return 4;
}
static double RollChance(Random rnd, int rolls)
{
var output = 0.0;
for (var n = 1; n <= rolls; n++)
output = Math.Max(output, rnd.NextDouble());
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment