title: Generating random numbers and inserting into array without repeats
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2351308/random-number-generator-in-c-sharp-unique-values">Random number generator in C# - unique values</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to write a C# program that will generate a random number, check if this number is in my array, if it is, repeat generating the number, else insert this number into slot <code>[i]</code> of my array.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>        int check = 0;
  
  
          Random rand = new Random();
  
          int[] lotto = new int[6];
  
  
  
          for (int i = 0; i &lt; lotto.Length; )
          {
              check = rand.Next(1, 41);
              while (!(lotto.Contains(check)))
              {
  
                  lotto[i] = check;
                  i++;
              }
  
              Console.WriteLine("slot " + i + " contains " + check);
  
          }
          Console.Read();
      }
  </code></pre>
  
  <p><strong>UPDATE:</strong> Thanks figured it out, replaced the if with while :)</p>