How to generate a random number in C#

This sample code generate 30,000 random 5-character-alphanumeric code in C#. The codes are stored in Hashset to be used for duplicate check.

 

Random random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
int length = 6;
int total = 30000;

HashSet<string> codes = new HashSet<string>();

Response.Write(DateTime.Now.ToString() + "<br>");
int d = 0;
for(int i=0;i<total;i++)
{
       string randomAlpha = new string(Enumerable.Repeat(chars, length) 
                              .Select(s => s[random.Next(s.Length)]).ToArray());

       if(codes.Contains(randomAlpha))
       {  
           Response.Write("found dup " + d);
           d++;
           i--;
          continue; // skip Add and generate again
      }
      codes.Add(randomAlpha);
}