1

I'm working with Visual Studio. I have an int array with 20 fields and I have a for-loop running 20 times to fill out all columns. It inserts the generated random numbers, and a while loop also looks if the random number exists in that array, if yes it's going to create a new random and run the while loop again and so on, until the for-loop is finished.

static int[] usedNumbers = new int[20];

public static void Show() {

for(int a = 0; a < 20; a++)
        {

            randomNr = Rnd.Next(0, 20);
            searchRnd = true;

            while (searchRnd)
            {
                if (usedNumbers.Contains(randomNr))
                {
                    randomNr = Rnd.Next(0, 20);
                    searchRnd = true;
                }
                else {
                    searchRnd = false;
                    // code...
                }
            }

        }
}




Show();
console.writeLine("{0}", usedNumbers[0]);

But for some reason when I run the console, this code isn't executed fully, I don't see nothing. Also the window doesn't close, it doesn't show the error message like it should when there is an error. Is there maybe a time limit or something like that?

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
abiih
  • 21
  • 3
  • 2
    "I don't see nothing". What do you expect to see? The code does not output anything. Please provide an [mcve]. The code you have shown is incomplete so it is not clear whether the missing parts are errors or intentionally left out - e.g. `usedNumbers`is never added to and `searchRnd` is never set to `false`. – kaylum Dec 26 '16 at 21:55
  • 1
    Please note that the random number you get back is going to be 0-19, 0 is already in your array as it is the default value. As such you can't add *20 unique numbers* to the array, only 19. Try using `Rnd.Next(1, 20)`. – Lasse V. Karlsen Dec 26 '16 at 21:56
  • And the code you gave above never sets searchRnd to false. – Rabid Penguin Dec 26 '16 at 21:57
  • if you are using this as console application add "Console.Read()" at the end to see the output of your application. You will need to 'console.write()' what you want – Fuzzybear Dec 26 '16 at 21:58
  • All symptoms reference to the infinite loop. – Fabio Dec 26 '16 at 22:04
  • The problem is that the usedNumbers[] isn't completely full at the end when I call it. – abiih Dec 26 '16 at 22:15
  • instead of being off-topic this should have been closed as duplicate to http://stackoverflow.com/questions/273313/randomize-a-listt – Alex P. Dec 26 '16 at 22:56

1 Answers1

1

There must be some code in your program which you haven't shown us, because as it is now, the outer for loop will exit the while loop immediately on each iteration. On the other hand, if the input array contains all of the integers 0 to 19, the while loop will never terminate.

It looks like what you are trying to achieve is the Fisher Yates Shuffle. This takes an array as an input, and randomises its order.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
  • 1
    Since the array by default contains all 0's, then the final attempt will never find a value not present in the array. There aren't 20 unique numbers from 1 through 19 inclusive. – Lasse V. Karlsen Dec 26 '16 at 22:26
  • I gave a couple of responses but deleted them as I was talking rubbish. Yes, it looks like that is the cause of the infinite loop. I suspect that changing the number of iterations to 19 will also fix the problem, though it is markedly less efficient than Fisher-Yates. – Rob Lyndon Dec 26 '16 at 22:44
  • I'm not sure why the question was put on hold -- it seems fairly clear and specific to me. – Rob Lyndon Dec 26 '16 at 22:45