11/25/2013 4:11:07 AM

The following will generate a random string of characters using .NET. You need to provide the number of random characters to generate. There is a generic function that accepts the number of characters and an array of possible values. The other functions simply use that generic function.

//Generic Function public static string GetRandomString(int numberofCharacters, string[] chars) { string value = ""; Random rnd = new Random(DateTime.Now.Millisecond); for (int x = 0; x < numberofCharacters; x++) { int index = rnd.Next(0, chars.Length); value += chars[index]; } return value; } public static string GetRandomNumericString(int numberofCharacters) { string[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; return GetRandomString(numberofCharacters, chars); } public static string GetRandomAlphaString(int numberofCharacters) { string[] chars = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0" }; return GetRandomString(numberofCharacters, chars); } public static string GetRandomAlphaNumericString(int numberofCharacters) { string[] chars = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; return GetRandomString(numberofCharacters, chars); }