.Net ramblings
# Tuesday, 15 March 2005
HowTo: get a random letter in C#
public static char GetRandomLowerCaseCharacter(int seed)
{
   return ((char) ( (short) 'a' + new Random(seed).Next(26)));
}

public static char GetRandomUpperCaseCharacter(int seed)
{
   return ((char) ( (short) 'A' + new Random(seed).Next(26)));
}

If you call the above methods straight after each other with the same seed, you may get the same value on a fast processor.  It is a good reason to pass in a different seed value for operations that will be done in quick succession.  E.g. use DateTime.Now.Seconds for one operation and then use Minutes or Hour or Milliseconds for the next ones.


Tuesday, 15 March 2005 18:26:27 (GMT Standard Time, UTC+00:00)  #    Comments [2]  .Net General