Weird Glitch (maybe) with random number generator

  • Thread starter Thread starter Jacob Gerow
  • Start date Start date
J

Jacob Gerow

Guest
Hi, as a part of one of my projects I am making a random number generator. The code for it is below. It takes a Maximum number, a list of numbers that should be excluded, and an array (I think that's the proper terminology I'm new) (to dump the numbers to and dictate how many numbers it should generate).

Anyways, when I run this slowly through the debugger, it works as expected. However, when I run it normally through visual studio without any breaks, I get numbers that are all right next to each other (example: 200, 202, 201, 203, 205), where I would be expecting a result like 305, 684, 126, 984, 235.

I don't know why this is happening and was wondering if I am doing anything wrong or if it's a glitch. Any help would be appreciated.

public static void GiveMeNumbers(int Max, List<int> exclude, int[] dumpNumbers)
{
List<int> excludeDup = new List<int>(exclude);

for (int j = 0; j < dumpNumbers.Length; j++)
{
var range = Enumerable.Range(0, Max + 1).Where(i => !excludeDup.Contains(i));
var rand = new System.Random();
int index = rand.Next(0, Max + 1 - excludeDup.Count);
dumpNumbers[j] = range.ElementAt(index);
excludeDup.Add(range.ElementAt(index));
}

}
You would use this like: GiveMeNumbers(1000, dontIncludeThisList, dumpNumbersHere);

Continue reading...
 
Back
Top