i have some chars:
chars = \"$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&\".ToCharArray();
now i\'m lookin
Well you're nearly there - you want to return a random element from a string, so you just generate a random number in the range of the length of the string:
public static char GetRandomCharacter(string text, Random rng)
{
int index = rng.Next(text.Length);
return text[index];
}
I'd advise against using a static
variable of type Random
without any locking, by the way - Random
isn't thread-safe. See my article on random numbers for more details (and workarounds).