How Long Will Your Smartphone Distract You From Family Dinner? I wrote a program to simulate this week's 538 Riddler, reproduced below: 


  You’ve just finished unwrapping your holiday presents. You and your
  sister got brand-new smartphones, opening them at the same moment. You
  immediately both start doing important tasks on the Internet, and each
  task you do takes one to five minutes. (All tasks take exactly one,
  two, three, four or five minutes, with an equal probability of each).
  
  After each task, you have a brief moment of clarity. During these, you
  remember that you and your sister are supposed to join the rest of the
  family for dinner and that you promised each other you’d arrive
  together. You ask if your sister is ready to eat, but if she is still
  in the middle of a task, she asks for time to finish it. In that case,
  you now have time to kill, so you start a new task (again, it will
  take one, two, three, four or five minutes, exactly, with an equal
  probability of each). If she asks you if it’s time for dinner while
  you’re still busy, you ask for time to finish up and she starts a new
  task and so on.
  
  From the moment you first open your gifts, how long on
  average does it take for both of you to be between tasks at the same
  time so you can finally eat? (You can assume the “moments of clarity”
  are so brief as to take no measurable time at all.)


And the program itself:

private static int numberOfSimulations = 100000;
static void Main(string[] args)
{
    var rand = new Random();
    var simulationTimes = new List&lt;int&gt;();
    for (int i = 0; i &lt; numberOfSimulations; i++)
    {
        var totalTime = 0;
        var myTaskTime = rand.Next(1, 6);
        var herTaskTime = rand.Next(1, 6);

        do
        {
            if (herTaskTime &gt; myTaskTime)
            {
                totalTime += myTaskTime;
                herTaskTime -= myTaskTime;
                myTaskTime = rand.Next(1, 6);
            }
            else if (herTaskTime &lt; myTaskTime)
            {
                totalTime += herTaskTime;
                myTaskTime -= herTaskTime;
                herTaskTime = rand.Next(1, 6);
            }
            else
            {
                totalTime += myTaskTime;
            }
        } while (myTaskTime - herTaskTime != 0);

        simulationTimes.Add(totalTime);
    }
    Console.WriteLine(simulationTimes.Average());
    Console.ReadLine();
}


Anecdotally, I'm consistently getting ~7.5 minutes.

I'm just looking for a general code review - does this look correct? Am I doing anything dumb? Should I have done it differently?
There's definitely an issue that will occur when a new task comes up with the same amount of time as what is left on the other person's task. Take for example the times.

MyTime | HerTime
----------------
3      | 5  


Now the first loop occurs, HerTime gets reduced to 2, TotalTime gets set to 3, then MyTime gets rerolled to 2. Now, your while condition hits as MyTime = HerTime and it breaks out, but those 2 minutes were never accounted for.

An easy fix would be to add a guard condition to your while that gets set in the Else part of your If.

(Warning, the below changes are untested air code)

private static int numberOfSimulations = 100000;
static void Main(string[] args)
{
    var rand = new Random();
    var simulationTimes = new List&lt;int&gt;();
    for (int i = 0; i &lt; numberOfSimulations; i++)
    {
        var totalTime = 0;
        var myTaskTime = rand.Next(1, 6);
        var herTaskTime = rand.Next(1, 6);
        var equal = false;

        do
        {
            if (herTaskTime &gt; myTaskTime)
            {
                totalTime += myTaskTime;
                herTaskTime -= myTaskTime;
                myTaskTime = rand.Next(1, 6);
            }
            else if (herTaskTime &lt; myTaskTime)
            {
                totalTime += herTaskTime;
                myTaskTime -= herTaskTime;
                herTaskTime = rand.Next(1, 6);
            }
            else
            {
                totalTime += myTaskTime;
                equal = true;
            }
        } while (!equal);

        simulationTimes.Add(totalTime);
    }
    Console.WriteLine(simulationTimes.Average());
    Console.ReadLine();
}