   Simple Simulation Code Fails-- Sometimes
  I've been fiddling with this for a few days, and I've run out of ideas. I'm practicing some basic C# programming, and I decided to make a simulation to test the Monty Hall problem (from a thread in the lounge a while back). I've been testing it with each feature I've added, and now I'm stuck.The code works as intended sometimes, but other times it hangs in the middle. The program is intended to create three boxes, randomly assign a prize to one, and randomly select one in phase one. This part works 100% of the time so far. In phase 2, a box that is neither chosen nor contains the prize is supposed to be eliminated. This code works, but only some of the time. When it works, it seems to do so perfectly-- eliminating an appropriate box every time. When it doesn't work, the program hangs after completing phase one.I've tried lots of different arrangements of my Eliminator function, but I always have the same, occasionally-works issue. I've looked it over over and over again, and I'm out of ideas of what might be causing it to fail sometimes. I'm confident that it's a round to issueHere's the code:namespace MontyHall{    class Program    {   static void Main(string[] args)   {  char b1 = 'A';  char b2 = 'B';  char b3 = 'C';  Box a = new Box(b1);  Box b = new Box(b2);  Box c = new Box(b3);  Box[] stage1 = new Box[3] {a, b, c};  Initialize.prizeStuffer(ref stage1);  Game.chooseBox(ref stage1);  Tester.showInfo(stage1);  Game.Eliminator(ref stage1);  Tester.showRound2(stage1);   }}}namespace MontyHall{    // Defines the box object type    public class Box    {   public bool prize;   public bool chosen;   public bool eliminated;   public char name;   // Constructor   public Box(char name)   {  // Set the box to empty  this.prize = false;  // Mark all boxes as unchosen  this.chosen = false;  // Set elimination flag to false  this.eliminated = false;  // Name the box  this.name = name;   }    }}namespace MontyHall{    public static class Game    {   // Randomly chooses one of the three boxes   public static void chooseBox(ref Box[] stage1)   {  // Get seed value from Seeder  int c = Seeder.Randomizer();  stage1[c].chosen = true;   }   // Eliminate a non-chosen, empty box   public static void Eliminator(ref Box[] stage1)   {  int i = 0;  bool keepon = true;  do  { i = Seeder.Randomizer(); if (stage1.chosen == false && stage1.prize == false) {stage1.eliminated = true;keepon = false; }  }  while (keepon == true);   }    }}// Randomly generates a number for box selectionnamespace MontyHall{    static class Seeder    {   static int seedGenerator()   {  Thread.Sleep(27);  Random seeder = new Random();  int seed = seeder.Next();  return seed;   }   public static int Randomizer()   {  Random random = new Random(seedGenerator());  int choice = random.Next(0, 2);  return choice;   }    }}// Places the prize in a random boxnamespace MontyHall{    public static class Initialize    {   public static void prizeStuffer(ref Box[] stage1)   {  int i = 0;  i = Seeder.Randomizer();  stage1.prize = true;   }    }}// Shows the results of each round, for testing purposesnamespace MontyHall{    public static class Tester    {   // Display the results of round 1   public static void showInfo(Box[] stage1)   {  for (int i = 0; i < stage1.Length; i++)  { Console.WriteLine("Box {0}:", stage1.name); Console.WriteLine("Prize: {0}", stage1.prize); Console.WriteLine("Chosen: {0}", stage1.chosen); Console.WriteLine("\n");  }   }   // Display the results of round 2   public static void showRound2(Box[] stage1)   {  for (int i = 0; i < stage1.Length; i++)  { if (stage1.eliminated == false) {Console.WriteLine("Box {0}:", stage1.name);Console.WriteLine("Prize: {0}:", stage1.prize);Console.WriteLine("Chosen: {0}", stage1.chosen); } else {Console.WriteLine("Box {0} was eliminated.", stage1.name); }  }   }    }}What am I missing? Clearly there's some problem above. Any help is appreciated. Any more general comments about my code are also welcome, as I'm sure I'm not using best practices.   
  Well what I've noticed so far is that you're using the random number generator wrong. You're creating an seeding a new generator each time, and hence have discovered a need to sleep so that the seeds aren't so similar or repeated. What you should do is have a member of type Random and only create this variable once, then Randomiser should just return random[color="#666600"].[color="#660066"]Next[color="#666600"]([color="#006666"]0[color="#666600"], [color="#006666"]2[color="#666600"]);[color="#000000"]You can then remove the Thread.Sleep();Secondly, a quick look at the documentation for Random.Next indicates that the upper bound is "exclusive" meaning that it does not include that value. Hence you actually need to return random[color="#666600"].[color="#660066"]Next[color="#666600"]([color="#006666"]0[color="#666600"], [color="#006666"]3[color="#666600"]);[color="#000000"]This would explain the lockup if you picked item 0 or 1 and item 1-choice was actually the prize, because it would be unable to eliminate any box. It would presumably work every time it you picked 2.   ;  Thanks for the reply! The parameters in my use of Random.Next() were what was causing the program to hang.As for my poor use of the random number generator, would this be better?namespace MontyHall{    static class Seeder    {   Random random = new Random();   public static int Randomizer()   {  int choice = random.Next(0, 3);  return choice;   }    }}The reason I did it as I did was because I'm planning on running the program for a large number of trials, and wanted to make sure that each trial was truly random so that the results aren't statistically unsound.   