//************************************** // Name: Random String in C# // Description:This is the simplest and most straightforward random string generator I could come up with. Have fun. (Please don't use this if you're too lazy to rate it.) // By: Jon Davis (from psc cd) // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //************************************** public static Random AppRandom = new Random((int)DateTime.Now.Ticks); public static string RandomString(int length, bool nums) { const string abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; const string abc123 = abc + "0123456789"; string charlist; if (nums) { charlist = abc123; } else { charlist = abc; } System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i=0;i<length;i++) { int r = AppRandom.Next(0, charlist.Length); sb.Append(charlist.Substring(r, 1)); } return sb.ToString(); }