I've written a BouncyCastle style threaded seeder and was wondering about the predictability of the data the program generates.
The reason I'm not simply using BouncyCastle is because security isn't a concern, I don't want to use a whole library just for one function, and this code is MUCH faster.
The idea is to make several threads which operate on a circular buffer (a simple array), where each thread performs a different operation on the data in the buffer. The randomness comes from thread scheduling irregularities in the OS.
How much randomness can I expect from a mechanism like this?
using System;
using System.Threading;
class ThreadedSeeder
{
static public ulong[] buffer = new ulong[1024];
static public bool doThread = true;
public ThreadedSeeder()
{
MakeThread(Add);
MakeThread(Multiply);
MakeThread(Xorshift);
Thread.Sleep(10);
doThread = false;
}
static private void MakeThread(ThreadStart a)
{
Thread b = new Thread(a)
{
Priority = ThreadPriority.Lowest
};
b.Start();
}
static private void Add()
{
int i = 0;
while (doThread)
{
buffer[i & 1023] += (ulong)DateTime.UtcNow.Ticks;
i++;
}
}
static private void Multiply()
{
int i = 0;
while (doThread)
{
buffer[i & 1023] *= 6364136223846793005;
i++;
}
}
static private void Xorshift()
{
int i = 0;
while (doThread)
{
ulong y = buffer[i & 1023];
y ^= (y << 13);
y ^= (y >> 17);
y ^= (y << 5);
buffer[i & 1023] = y;
i++;
}
}
}
Aucun commentaire:
Enregistrer un commentaire