title: Call function repeatedly until desired output is received
body: <p>I'm making an automated stat roller of sorts and I want it to keep running until I get the desired output. Here's the code:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          _getStats();
      }
  
      static void _getStats()
      {
          Random rnd = new Random();
          int stat1 = 0;
          int stat2 = 0;
          int stat3 = 0;
          int stat4 = 0;
          int stat5 = 0;
          int stat6 = 0;
  
          for (int i = 0; i &lt; 6; i++)
          {
  
              int roll1 = rnd.Next(1, 7);
              int roll2 = rnd.Next(1, 7);
              int roll3 = rnd.Next(1, 7);
              int roll4 = rnd.Next(1, 7);
              int tempStatTotal = 0;
              List&lt;int&gt; rollList = new List&lt;int&gt;();
  
              rollList.Add(roll1);
              rollList.Add(roll2);
              rollList.Add(roll3);
              rollList.Add(roll4);
              rollList.Sort();
              rollList.RemoveAt(0);
  
              foreach (int j in rollList)
              {
                  tempStatTotal += j;
              }
  
              if (i == 0)
              {
                  stat1 = tempStatTotal;
              }
              else if(i == 1)
              {
                  stat2 = tempStatTotal;
              }
              else if (i == 2)
              {
                  stat3 = tempStatTotal;
              }
              else if (i == 3)
              {
                  stat4 = tempStatTotal;
              }
              else if (i == 4)
              {
                  stat5 = tempStatTotal;
              }
              else
              {
                  stat6 = tempStatTotal;
              }
          }
  
          if(stat1 == 18 || stat2 == 18 || stat3 == 18 || stat4== 18 || stat5 == 18 || stat6 == 18)
          {
              Console.WriteLine(stat1);
              Console.WriteLine(stat2);
              Console.WriteLine(stat3);
              Console.WriteLine(stat4);
              Console.WriteLine(stat5);
              Console.WriteLine(stat6);
          }
          else
          {
              Main(null); //Start the process all over again until one of the stats is 18
          }
      }
  }
  </code></pre>
  
  <p>When I run this code, on a few occasions, the Console logs 6 numbers and 1 of them is 18. Most of the time, the applications breaks and the error <code>System.StackOverflowException</code> is thrown. I understand this has something to do with infinite loops and it's happening because I'm recursively calling my method but how do I work around this?</p>