bitshift wrote:
Anyone got an example ?
Also,
///
using System;
using System.Text;
....
public string GetRandomString (Random rnd, int length)
{
string charPool
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz1234567890";
StringBuilder rs = new StringBuilder();
while (length-- 0)
rs.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]);
return rs.ToString();
}
///
Watch out for wrapping on the charPool line! Note, the method takes an
instance of the Random class, which you can instantiate with new Random().
The reason it doesn't do this itself is that (I think) the Random class uses
a timer as it's entropy source, and if you try to generate a large sequence
of random numbers quickly, the seeds will match and you'll get matching
random sequences.
So the way I tested this was:
///
public static void Main ()
{
int i = 50;
Random rnd = new Random();
while (i-- 0)
Console.WriteLine(GetRandomString(rnd, 10));
}
///
--
Tom Spink
University of Edinburgh