長さ 9 のランダムな文字列を生成したい。
10~15回程度衝突するコードです。同じ文字列を返すランダム文字列ジェネレーターの功績。本当にランダムな文字列を生成するのを手伝ってくれる人はいますか?
class Program
{
private static Random random = new Random((int)DateTime.Now.Ticks);
private static object locker = new object();
private static string RandomString(int size)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
lock (locker)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
}
builder.Append(ch);
}
return builder.ToString();
}
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
object locker2 = new object();
ThreadPool.QueueUserWorkItem(new WaitCallback((obj) => {
for (int i = 0; i < 5000000; i++)
{
string random = RandomString(9);
lock (locker2)
{
if (!dict.ContainsKey(random))
dict[random] = random;
else
Console.WriteLine("Found");
}
}
}));
ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
{
for (int i = 0; i < 5000000; i++)
{
string random = RandomString(9);
lock (locker2)
{
if (!dict.ContainsKey(random))
dict[random] = random;
else
Console.WriteLine("Found");
}
}
}));
ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
{
for (int i = 0; i < 5000000; i++)
{
string random = RandomString(9);
lock (locker2)
{
if (!dict.ContainsKey(random))
dict[random] = random;
else
Console.WriteLine("Found");
}
}
}));
ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
{
for (int i = 0; i < 5000000; i++)
{
string random = RandomString(9);
lock (locker2)
{
if (!dict.ContainsKey(random))
dict[random] = random;
else
Console.WriteLine("Found");
}
}
}));
Console.ReadKey();
}
}