Generate Random string

This sample shows how to generate a random string.


public string GetRandomNumbers(int numChars)
{
string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z","0","1", "2", "3", "4", "5", "6", "7", "8", "9" };

Random rnd = new Random();
string random = string.Empty;
for (int i = 0; i < numChars; i++)
{
random += chars[rnd.Next(0, 33)];
}
return random;
}


Comments

Guest Author: Vijay05 Jan 2012

Good Work Brother it works perfectly. I have one doubt. First i passed 4 as argument to GetRandomNumbers(), i get output as "EQYZ". Then i passed 8 as argument to GetRandomNumbers(), i get output as "UAPNVJTT". Here 'T' is repeating. Is there is any other way to display only distinct characters.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: