simple:
const string _availChars = "abcdefghijklmnopqrstuvwxyz0123456789";
string GenAlphaNum(int length){
var random = new Random();
return new string(Enumerable.Repeat(_availChars , length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
You can toy with the idea of having Caps added to the list of available chars but that has the drawback of making 'A' as likely as 'a' as likely as any other single number. One way around this it to add the number string twice to even up the odds, or find some clever way of upper-casing random chars in the string.
There's loads of examples out these. This is pretty similar to a javascript version I use but I haven't tested this in C#
UPDATE:
In case you are not aware of how to add a seed - here are a couple of examples
const string _availChars = "abcdefghijklmnopqrstuvwxyz0123456789";
string GenAlphaNum(int length)
{
var random = new Random(DateTime.Now.Millisecond);
return new string(Enumerable.Repeat(_availChars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
with constant random
const string _availChars = "abcdefghijklmnopqrstuvwxyz0123456789";
Random _random = null;
Random Rand
{
get
{
if (_random == null)
_random = new Random(DateTime.Now.Millisecond);
return _random;
}
}
string GenAlphaNum(int length)
{
return new string(Enumerable.Repeat(_availChars, length)
.Select(s => s[Rand.Next(s.Length)]).ToArray());
}