滞后的Fibonnaci Algorthm

时间:2016-05-15 13:16:36

标签: c# arrays algorithm generator

我有这个错误:当我尝试调用任何方法时,对象引用未设置为对象的实例,在这种情况下,我尝试调用Next方法。

至于我的LaggedFibonnaci课程中的代码我有:

    int[] initialsetofvalues;
    int j = 30;
    int k = 127;
    int m = 2 ^ 31;
    int n;

    public LaggedFibonnaci()
    {
        int[] initialsetofvalues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    }

    public int Next(int maxValue)
    {
        n = k;
        int randNo = (initialsetofvalues[n - j] + initialsetofvalues[n - k]) % m;
        n++;
        return randNo % (maxValue + 1);
    }


    public int NextWithDiscarding(int maxValue)
    {
        Random rand = new Random();

        //Generate 127 Random Numbers
        for (int i = maxValue - 1; i > 0; i--)
        {
            int randNumbers = rand.Next(1, 128);
            randNumbers = initialsetofvalues[maxValue];
        }


        //Generate 997 Random Numbers
        for (int i = maxValue - 1; i > 0; i--)
        {
            int randNumbers = rand.Next(1, 998);
            randNumbers = initialsetofvalues[maxValue];
        }

        return Convert.ToInt32(rand);

    }

    public int NextWithRandomShuffling(int maxValue)
    {
        int[] v = new int[61];

        for (int i = 0; i < 61; i++)
        {
            v[i] = Next(maxValue);
        }

        int y = Next(maxValue);
        int rand = (61 * y) / m;
        v[rand] = y;
        return y;

    }
}

我还从接口IRandomProvider实现了一个maxValue,代码为:

public interface IRandomProvider
    {
        int Next(int maxValue);
    }

是的,laggedfibonnaci类继承自此接口。

程序代码:

{
        static void Main(string[] args)
        {
            LaggedFibonnaci lfn = new LaggedFibonnaci();
            int[] initialsetofvalues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

           int array = lfn.Next(2);

            Console.WriteLine(array);
            Console.ReadLine();


        }
    }

任何帮助?感谢

1 个答案:

答案 0 :(得分:1)

更改构造函数:

public LaggedFibonnaci()
{
    this.initialsetofvalues = new[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}

您创建了一个本地列表而不是实例列表。