Generation of random alphanumeric string in C#

The following code snippet shows how to generate random alphanumeric string in C#.

using System;
using System.Text;

public string RandomString (Random r, int len)
string str
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder sb = new StringBuilder();

while ((len--)> 0)
sb.Append(str[(int)(r.NextDouble() * str.Length)]);

return sb.ToString();


Author: Sam31 Oct 2008 Member Level: Bronze   Points : 1

Shanthi M

Can you please check this link.....

If you copied some one code , provide link....



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