RSS 2.0 | Atom 1.0 | CDF

Search

Categories

Archive

Blogroll

Sign In

# Tuesday, March 15, 2005
Tuesday, March 15, 2005 6:26:27 PM (GMT Standard Time, UTC+00:00) ( .Net General )
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.

Comments [2] | | # 
Thursday, July 08, 2010 10:43:25 PM (GMT Daylight Time, UTC+01:00)
You should create a new instance of the Random class outside of the function and then pass that into the function instead of the seed. You shouldn't re-seed the the Random class for each call to it in a loop. An example:

string code = string.Empty;

for (int i = 1; i < 6; i++)
{
code += ((char)((short)'A' + random.Next(26)));
}
John Vogel
Friday, May 27, 2011 6:51:38 PM (GMT Daylight Time, UTC+01:00)
This helped me out, thanks!
Max
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview