lundi 25 juin 2018

Lagged Fibonacci RNG Returning negative

I have this code for a lagged fibonacci RNG in c-sharp. Some of the results i'm getting from this code are negative and or duplicates. Do you know the issue with this please?

    int[] initialSetofKStates;
    //Declare Values for j, k and m
    int n = 0;
    int k = 89;
    int j = 38;
    double M = Math.Pow(2,32);

    public LaggedFibonacciRNG()
    {
        //Set n equal to k
        n = k;
        //Initialize the initial set of K to hold k Random Numbers
        initialSetofKStates = new int[k];
        //Fill the initial set of K states with Random Numbers
        Random r = new Random();
        for (int i = 0; i < initialSetofKStates.Length;i++)
        {
            initialSetofKStates[i] = r.Next();
        }
    }

    public decimal generateRandomNumber(int max)
    {

        if (k <= 1) k = 89; else k--;
        if (j <= 1) j = 38; else j--;

        int rand1 = initialSetofKStates[n - k];
        int rand2 = initialSetofKStates[n - j];
        long Random = (long)((rand1 + rand2) % M);
        initialSetofKStates[n-k] = (int)Random;

        return (decimal) Math.Floor((Random * max) / M);
    }




Aucun commentaire:

Enregistrer un commentaire