Random String Generation using C#

Get random string.


This code demonstrate how to get random number using C#.

Get random string from char A to Z having desired length of string.
call this function like below.

int num=5;
string str= GetRandomString(num);

Function for getting random number

public string GetRandomString(int strlength)
Random rnd = new Random
string strRandom = "";
string strNextRandom = "";
int strNextNumber = 0;
for (int i = 0; i < strlength; i++)
strNextNumber = rnd.Next(65, 90);();
//Get next number using random function between 65 to 90.
strNextRandom = new string((Char)strNextNumber, 1);
//convert to ascii value to char
strRandom += strNextRandom;
return strRandom.ToString();


No responses found. Be the first to comment...

  • 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: