blog.x-e.ro / asp.net random number generator : the central randomizer

asp.net random number generator

c-sharp :: the central randomizer

here is my asp.net randomizer class based on the c R250/512 shift-register sequence random number generator, by kirkpatrick and stoll and published (j. computational physics, vol 40, pp. 517-526) with an added a pseudo-random class redefinition and buffer overflow protection.

public sealed class randomizer {
    private int good_index;
    private int evil_index;
    private int[] good_buffer = new int[250];
    private int[] evil_buffer = new int[521];
 
    public randomizer()
    {
        Random r = new Random();
        int i = 521;
        int seed = 1;
        int salt = 999999999;
 
        while (i-- > 250) {
            evil_buffer[i] = r.Next();
        }
        while (i-- > 31) {
            good_buffer[i] = r.Next();
            evil_buffer[i] = r.Next();
        }
        while (i-- > 0) {
            good_buffer[i] = (r.Next() | seed) & salt;
            evil_buffer[i] = (r.Next() | seed) & salt;
            salt ^= seed;
            seed >>= 1;
        }
        good_buffer[0] = seed;
        evil_buffer[0] = salt;
        good_index = 0;
        evil_index = 0;
    }
 

    public int random() {
        int g = good_index;
        int e = evil_index;
 
        int nina = g - (250-103);
        if (nina < 0)
            nina = g + 103;
        int xero = e - (521-168);
        if (xero < 0)
            xero = e + 168;
 
        int x = good_buffer[nina] ^ good_buffer[g];
        good_buffer[g] = x;
        int z = evil_buffer[xero] ^ evil_buffer[e];
        evil_buffer[e] = z;
 
        g = (g != 249) ? (g + 1) : 0;
        good_index = g;
        e = (e != 521) ? (e + 1) : 0;
        evil_index = e;
 
        return x ^ z;
    }

}


example usage

randomizer x = new randomizer();
int num = x.random();
loading...

loading...