-1

I can't understand what's wrong.

     static int WinningColumn()
    {
        Random rnd = new Random(46);

        int[] winningnumbers = new int[6];
        int[] Check = new int[46];
        int i;
        for (i = 0; i < winningnumbers.Length; i++)
        {
            winningnumbers[i] = rnd.Next(46);
            Check[winningnumbers[i]]++;

            if (Check[winningnumbers[i]] > 1)
            {
                i--;
                continue;
            }

The error happens here:

        }
        return winningnumbers[i];
       }
halfer
  • 19,824
  • 17
  • 99
  • 186
NateS
  • 33
  • 6
  • Useful posting tips: (1) always search for errors here and on your favourite search engine before posting, (2) refrain from adding 'help please' anywhere in your post, and especially in your title, as it is generally viewed as a form of begging; (3) use useful titles, such as `How to trace why an IndexOutOfRange exception occurs in a C# program?`, (4) research the problem yourself if possible, and show us what you have tried to debug it. Hope that helps. – halfer Sep 18 '16 at 10:18

2 Answers2

1

When you exit from the for loop the indexer variable i has a value bigger than the max index possible (It is the condition that breaks the loop).
In your case the variable i has the value of 6 but the max index possible for the array winningnumbers is 5. (0 to 5 are six integer elements).

It is not clear what is your intent but supposing that your purpose is to generate six winning numbers ranging from 0 to 45 then your code should be rewritten and simplified in this way

static List<int> WinningColumn()
{
    // Do not initialize Random with a fixed value
    // You will get always the same 'random' sequence
    Random rnd = new Random();

    // Create a list to store the winners
    List<int> winningnumbers = new List<int>();
    int i = 0;
    while(i < 6)
    {
        int newNumber = rnd.Next(46);
        if(!winningnumbers.Contains(newNumber))
        {
            // If the list doesn't contain the number the add it and increment i
            // Otherwise run the loop again....
            winningnumbers.Add(newNumber);
            i++;
        }
    }

    // This returns the whole list to the caller, 
    // you can use it as an array 
    return winningnumbers;
}

Notice that your actual code contains a bug in the declaration of the Random number generator. You pass an initial seed and thus, everytime you call this method, the random generator starts again with the same sequence of numbers. The result would be an identical list of numbers. Not very random to me
If you don't pass anything then the generator is initialized with the system time and thus should be different every time you call this method.

Steve
  • 213,761
  • 22
  • 232
  • 286
0

I don't know what you want to achieve here.

But the loop would terminate when i becomes 6. So you are basically trying to access winningnumbers[6] which is incorrect since winningnumbers array has length 6 so you can use index from 0 till 5.

You may try with winningnumbers [i-1]

Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98