IndexOutOfRangeException ?
<p>In this following code i am receiving and out of range exception.</p>

<pre><code>private void btnRoll_Click(object sender, EventArgs e)
    {
        int success4 = 0;
        int success6 = 0;
        int success8 = 0;
        int success10 = 0;
        int success20 = 0;
        int botch4 = 0;
        int botch6 = 0;
        int botch8 = 0;
        int botch10 = 0;
        int botch20 = 0;

        if (cbnd4.SelectedIndex != 0)
        {

            int value = 4;
            int arraySize = (int)cbnd4.SelectedIndex;
            int[] refArray = randomNumber(value, arraySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] &gt;= 2)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch4++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if(refArray[i] &gt;= 2)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch4++;
                    }
                }


            }
        }
           /* if (cbmd4.SelectedIndex != 0)
            {
            }
            */
         if (cbnd6.SelectedIndex != 0)
        {
            int value = 6;
            int arrySize = (int)cbnd6.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {


                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] &gt;= 4)
                    {
                        success6++;
                    } if (refArray[i] == 1)
                    {
                        botch6++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] &gt;= 4)
                    {
                        success6++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch6++;
                    }
                }


            }
        }
        if (cbnd8.SelectedIndex != 0)
        {
            int value = 8;
            int arrySize = (int)cbnd8.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] &gt;= 5)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch8++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] &gt;= 5)
                    {
                        success4++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch8++;
                    }
                }


            }
        }
        if (cbnd10.SelectedIndex != 0)
        {
            int value = 10;
            int arrySize = (int)cbnd10.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] &gt;= 7)
                    {
                        success10++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch10++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] &gt;= 7)
                    {
                        success10++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch10++;
                    }
                }


            }
        }
        if (cbnd20.SelectedIndex != 0)
        {
            int value = 20;
            int arrySize = (int)cbnd20.SelectedIndex;
            int[] refArray = randomNumber(value, arrySize);
            foreach (int i in refArray)
            {

                if (cbGame.SelectedIndex == 1)
                {
                    if (refArray[i] &gt;= 16)
                    {
                        success20++;
                    }
                    if (refArray[i] == 1)
                    {
                    botch20++;
                    }
                }
                if (cbGame.SelectedIndex == 2)
                {
                    if (refArray[i] &gt;= 7)
                    {
                        success20++;
                    }
                    if (refArray[i] == 1)
                    {
                        botch20++;
                    }
                }


            }
        }
        lBotch_Result.Text = Convert.ToString(botch4 + botch6 + botch8 + botch10 + botch20);
        lSuccess_Result.Text = Convert.ToString(success4 + success6 + success8 + success10 + success20);
        MessageBox.Show("d4 successes: " +
            success4.ToString() +
            "\r\nd6 Successes: " +
            success6.ToString() +
            "\r\nd8 Successes: " +
            success8.ToString() +
            "\r\nd10 Successes: " +
            success10.ToString() +
            "\r\nd20 Successes: " +
            success20.ToString() +
            "\r\nd4 Botches: " +
            botch4.ToString() +
            "\r\nd6 Botches: " +
            botch6.ToString() +
            "\r\nd8 Botches: " +
            botch8.ToString() +
            "\r\nd10 Botches: " +
            botch10.ToString() +
            "\r\nd20 Botches: " +
            botch20.ToString());

}
</code></pre>

<p>The Out of ranged exception occurs when if(refArray[i] >= 7) and the refArray.Length contains an odd int value.</p>

<p>here is the Exception Output: </p>

<blockquote>
  <p>System.IndexOutOfRangeException was
  unhandled<br>
  Message="IndexOutOfRangeException"<br>
  StackTrace:
         at Table_Top_Game_Dice.Form1.btnRoll_Click(Object
  sender, EventArgs e)
         at System.Windows.Forms.Control.OnClick(EventArgs
  e)
         at System.Windows.Forms.Button.OnClick(EventArgs
  e)
         at System.Windows.Forms.ButtonBase.WnProc(WM
  wm, Int32 wParam, Int32 lParam)
         at System.Windows.Forms.Control._InternalWnProc(WM
  wm, Int32 wParam, Int32 lParam)
         at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr
  hwnMain)
         at System.Windows.Forms.Application.Run(Form
  fm)
         at Table_Top_Game_Dice.Program.Main()</p>
</blockquote>

<p>any advice here would be greatly appreciated. i have been pounding my head against the wall for 5 hours trying to fix this.</p>

<p>oh, the refArray gets it values from the following function: (if it helps)</p>

<pre><code>private int[] randomNumber(int value, int arraySize)
    {
        int[] randArray = new int[arraySize];
        maxValue = value;
        Random rand = new Random();
        for (int i = 0; i &lt; arraySize; i++)
        {
           randArray[i] = rand.Next(minValue, maxValue);
        }
        return randArray;
    }
</code></pre>
