A little late-night coding

in #gaming5 years ago

You know how it is. It's getting late, you should be in bed, but your brain just decides to go all crazy on you and decide "It's time to code!" And it's never anything super important either. Well, tonight I just wrote a little console app to determine some dice rolling probability.

Now I know there are some sites out there that do this already like AnyDice.com. You can put in whatever dice you want, and the site will tell you the probability of each roll. Like so:
anydice

Well I was feeling extra nerdy and decided to try to write my own for a rather specific roll: rolling 4 6-sided dice, drop the lowest, and add them together. In case you weren't aware, this is the typical way ability scores are rolled in Dungeons & Dragons. And so I wasted plenty of time doing so.

It was a little tricky, but here's the output.
image.png

So the top 5 are: 13, 12, 14, 11, 15. This is much higher than the top 5 for 3d6 rolling: 9-12 are all really close. And while rolling 3d6 you have an equally small chance to roll a 3 as you do an 18. With the 4d6 and drop lowest, you are way more likely to get an 18.

For fun, here's the code.

using System.Linq;

namespace StatRollerProbability
{
    class Program
    {
        
        static void Main(string[] args)
        {

            int[] tallies = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int[] roll = { 1, 1, 1, 1 };
            int currentDie = 0;
            bool done = false;

            Console.WriteLine("Hello Fellow Gamers!!");
            Console.WriteLine("Today we are gonna see the odds for the more herioc dice rolling for stats in DnD!!");
            Console.WriteLine("This is where you roll 4d6, drop the lowest, and add them together for each stat.");
            Console.WriteLine("With just 3d6, you have a 1 in 216 chance to get an 18, and the same odds to get a 3. 10 and 11 are the most common rolls.");
            Console.WriteLine("With 4d6, there are 1296 combinations.");
            Console.WriteLine("How much better is it in this more heroic method?");
            Console.WriteLine("   ");

            while (!done || currentDie > 3)
            {
                tallies[DiceAdder(roll)]++;
                DiceTicker(ref roll, ref currentDie, ref done);
            }
            
            for(int x=3;x<tallies.Length;x++)
            {
                Console.Write(" "+ (x < 10 ? " " : "") + x + ":");
                for (int p=0; p < tallies[x]; p++)
                {
                    Console.Write("x");
                }
                Console.WriteLine("");
            }

        }

        
        private static void DiceTicker(ref int[] roll, ref int currentDie, ref bool done)
        {
            roll[0]++;
            if (roll[0] >= 7)
            {
                roll[0] = 1;
                roll[1]++;
                if (roll[1] >= 7)
                {
                    roll[1] = 1;
                    roll[2]++;
                    if (roll[2] >= 7)
                    {
                        roll[2] = 1;
                        roll[3]++;
                        if (roll[3] >= 7)
                        {
                            done = true;
                        }
                    }
                }
            }
        }


        private static int DiceAdder(int[] roll)
        {
            return roll.Sum() - roll.Min();

        }

    }
}