README.md

December 4, 2022 ยท View on GitHub

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.)

More Info

Submitted On
ByJon Davis
LevelIntermediate
User Rating4.4 (35 globes from 8 users)
CompatibilityC#
CategoryStrings
World.Net (C#, VB.net)
Archive File

Source Code

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();
}