dimanche 12 février 2017

WELL 512 algorithm in Threadsafe using c# Static varaiables

My Code for Well512 algorithm in C# given below using constructor i will generate the seed value from Datetime.now.ticks,i want to convert it into threadsafe,for accessing multiplethreads instance of this class,

public  class Well512
        {
            static uint[] state = new uint[16];
            static uint index = 0;
            static Well512()
            {
                Random random = new Random((int)DateTime.Now.Ticks);

                for (int i = 0; i < 16; i++)
                {
                    state[i] = (uint)random.Next();
                }
            }
            internal static uint Next(int minValue, int maxValue)
            {
                uint rand;
                int range = maxValue - minValue + 1;
                long MAX_RANGE = ((long)1) << 32;
                long limit = (MAX_RANGE - (MAX_RANGE % range));
                do
                {
                    rand = Next(); 
                } while (rand >= limit);

                return (uint)((rand % range) + minValue);
            }
            public static uint Next()
            {
                uint a, b, c, d;
                a = state[index];
                c = state[(index + 13) & 15];
                b = a ^ c ^ (a << 16) ^ (c << 15);
                c = state[(index + 9) & 15];
                c ^= (c >> 11);
                a = state[index] = b ^ c;
                d = a ^ ((a << 5) & 0xda442d24U);
                index = (index + 15) & 15;
                a = state[index];
                state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
                return state[index];
            }



        }




Aucun commentaire:

Enregistrer un commentaire