title: C# - checking for duplicates in a lottery program
body: <p>I have to write a lottery program which produces 6 numbers between 1 and 40 and checks for duplicates. I have no problem with generating random numbers but it's the algorithm that checks them that I'm having trouble with. Apart from the bugs below, the code seems to work (I have run the program many times and have never gotten any number as a duplicate (except zero, see below).</p>
  
  <p>These are the bugs.</p>
  
  <ol>
  <li>I occasionally get two zeroes as the last two numbers.</li>
  <li>One zero occasionally appears as the last number (but never anywhere else except in bug #1.</li>
  </ol>
  
  <p>Here is the source code: </p>
  
  <pre><code>using System;
  using System.Linq;
  
  namespace LottoV2Program
  {
      class LottoV2
      {
           static void Main(string[] args)
          {
              //declare variables
               int[] lottoNumbers = new int[6];
              Random rand = new Random();
              int temp = 0;
              int count = 0;
  
              //fill array with lottery numbers
               for (int i = 0; i &lt; lottoNumbers.Length; i++)
              {
                  temp = rand.Next(1, 41);//generate random number
  
                   //check to see whether number has already been picked
                  while (lottoNumbers.Contains(temp) == false)
                  {
                      lottoNumbers[count] = temp;
                      count++;
                  }
  
              }
  
              //display numbers on screen, one per line
              foreach (int i in lottoNumbers)
              {
                  Console.WriteLine(i);
              }
  
              //pause
              Console.ReadLine();
          }//end mm
  </code></pre>