   Why is this an infinite loop?
  do {Random random = new Random();nextNumber = random.Next(1, Walls.Length); } while (DownQueue.Contains(nextNumber));I don't get it, it doesn't get a compiler error but the loop seems to go on forever when I run the program. Someone help me understand what the heck's going on?   
  Why don't you start a breakpoint and see what the values are at various points in the code?   ;  Well if your "nextNumber" is always found in DownQueue, it is possible that this loop runs forever   ;  nvm I found the answer, apparently the MaxNumber parameter for Random.Next has to be 1 larger than the maximum possible number you have in mind.   ;  Array indices range from 0 to N-1, so it's usual to generate random numbers in those kinds of ranges, too. Especially when your "range" depends on the length of an array. I assume you will later be using the selected number to index into the Walls array. If you were just going to use it as is, the code would break on occasion (array index out of bounds), and element 0 would never be picked. Even if you knew to subtract 1, you're going out of your way to make the code more complicated. :\   ;  Maybe the random object always delivers the same random number? I suggest you put the creation of the random object outside of the loop, even if that isn't the case.Random random = new Random();do{    nextNumber = random.Next(1, Walls.Length);} while (DownQueue.Contains(nextNumber));;Quote:Original post by DevFredMaybe the random object always delivers the same random number? I suggest you put the creation of the random object outside of the loop, even if that isn't the case.This is true. Random, by default, uses the time as its seed. Your loop is sufficiently tight that the resolution of the time source used is not fine enough that the time used doesn't change between initializations. This isn't necessarily your problem (seems not to be, and would be unlikely as the time read would change after a very short time) but it will smack you in the face if you don't know it.   ;  A further reason for moving the creation outside the loop is that you've created a nasty-ish memory leak. Even if you are using a garbage-collected language, you're quite un-necessarily giving the garbage collector lots of work, which may slow down the game in unpredictable places. Please note, I'm not saying that this particular loop is actually going to be a problem; it probably exits fast enough that the added memory use is negligible. But this sort of frequent object creation is a bad habit in general, and should be avoided where possible; especially in situations like this where it may actually lead to logic problems!   ;  Thanks for the help guys but I've already got it figured out.  for (int i = 0; i < Walls.Length; i++)  { do {Random random = new Random();nextNumber = random.Next(1, Walls.Length); } while (DownQueue.Contains(nextNumber)); DownQueue.Enqueue(nextNumber);  }This was the actual neverending loop, the problem was that the MaximumNumber parameter in random.Next has to be one larger than the one you have in mind. So if Walls.Length was 13, then the largest possible number would be 12. So the for loop would iterate 13 times and there would only be 12 possible numbers for Random.Next, so through the 13th iteration, the DoWhile loop would never end because Random.Next can never find a value that isn't already in the DownQueue. I just changed the Minimum Number to 0 and fixed it.Also, the reason I put Random in the do..while loop is cause I guessed at first the reason for the infinite loop was that random.Next always returned the same value, so I put random in the loop to make sure it's always a new instance returning a new value. I've fixed it now.   ;Quote:Original post by Menace2SocietyAlso, the reason I put Random in the do..while loop is cause I guessed at first the reason for the infinite loop was that random.Next always returned the same value, so I put random in the loop to make sure it's always a new instance returning a new value. I've fixed it now.I'm not certain what you mean by "I've fixed it now"... have you put it outside of the loop? Let me explain:Do you know how random numbers work? When you create a Random object, it is initialized with a seed. This seed determines the sequence of random numbers you get on subsequent calls to Next. If you create an object x and an object y with the same seed, the first call to x.Next and the first call to y.Next will yield the SAME number.If you don't specify the seed yourself, modern languages tend to compute it from the current time. This current time has a certain granularity, for example a millisecond. Now if you create severel objects within this interval, they will all produce the same sequence of numbers. And since you are only using the first number of these sequences, it takes an entire millisecond before you get another number.That's why it is incorrect to put the creation of the random object INTSIDE of the loop - this will make the numbers LESS RANDOM.