DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Build A Random String

07.27.2005
| 8794 views |
  • submit to reddit
        
/// <summary>
/// Build a random string (for id, login, password...)
/// </summary>
public static string randomString() {
	int length = new Random().Next(6, 10);
	return randomString(length);
}

/// <summary>
/// Build a random string (for id, login, password...)
/// </summary>
public static string randomString(int length) {
	string tempString = Guid.NewGuid().ToString().ToLower();
	tempString = tempString.Replace("-", "");
	while (tempString.Length < length) {
		tempString += tempString;
	}
	tempString = tempString.Substring(0, length);
	return tempString;
}