c#for循环算术

时间:2012-11-06 15:28:36

标签: c#

我为课程编写的这个程序使用“随机”数字来生成作业的到达(到达[i])和服务时间(service [i])。我目前的问题是抵达时间。为了得到到达时间,我调用一个名为exponential的函数,并将返回值添加到数组中的上一个到达时间(到达[i-1])。由于某些原因我不明白,程序没有使用数组的先前值进行添加,而是使用看似随机的值(1500,1600等)。但我知道数组中设置的实际值都低于5.这应该是for循环中的简单数组算法,但我无法弄清楚出了什么问题。

namespace ConsoleApplication4
{

class Program
{
    static long state;

    void putseed(int value)
    {
        state = value;
    }

    static void Main(string[] args)
    {
        Program pro = new Program();
        double totals = 0;
        double totald = 0;
        pro.putseed(12345);
        double[] arrival = new double[1000];
        double[] service = new double[1000];
        double[] wait = new double[1000];
        double[] delay = new double[1000];
        double[] departure = new double[1000];
        for (int i = 1; i < 1000; i++)
        {
            arrival[i] = arrival[i - 1] + pro.Exponential(2.0);
            if (arrival[i] < departure[i - 1])
                departure[i] = departure[i] - arrival[i];
            else
                departure[i] = 0;
            service[i] = pro.Uniform((long)1.0,(long)2.0);
            totals += service[i];
            totald += departure[i];
        }
        double averages = totals / 1000;
        double averaged = totald / 1000;
        Console.WriteLine("{0}\n",averages);
        Console.WriteLine("{0}\n", averaged);
        Console.WriteLine("press any key");
        Console.ReadLine();
    }

    public double Random()
    {
        const long A = 48271;
        const long M = 2147483647;
        const long Q = M / A;
        const long R = M % A;
        long t = A * (state % Q) - R * (state / Q);
        if (t > 0)
            state = t;
        else
            state = t + M;
        return ((double)state / M);
    }

    public double Exponential(double u)
    {
        return (-u * Math.Log(1.0 - Random()));
    }

    public double Uniform(long a, long b)
    {
        Program pro = new Program();
        double c = ((double)a + ((double)b - (double)a) * pro.Random());
        return c;
    }
}

}

4 个答案:

答案 0 :(得分:1)

Exponential方法返回的值可能非常大。非常非常大。事实上,如果你的Random值接近于1,它们会趋于无穷大......

我并不觉得你到达阵列的价值往往很大。我实际上期望他们。

另外:尝试根据他们的行为命名您的方法。您的Exponential方法与数学指数无关。

尽量不要自己实现随机数生成器。使用.Net Framework中包含的Random类。如果你想总是拥有相同的伪随机数序列(你似乎想要),你可以使用常量对它进行种子化。

答案 1 :(得分:1)

根据您当前的逻辑,您的输出听起来完全正确。也许你的逻辑存在缺陷?

我将for循环的前三行更改为:

var ex = Exponential(2.0);
arrival[i] = arrival[i - 1] + ex;
Console.WriteLine("i = " + arrival[i] + ", i-1 = " + arrival[i-1] + ", Exponential = " + ex);

这是输出的开始和结束:

i = 0.650048368820785, i-1 = 0, Exponential = 0.650048368820785
i = 3.04412645597466, i-1 = 0.650048368820785, Exponential = 2.39407808715387
i = 4.11006720700818, i-1 = 3.04412645597466, Exponential = 1.06594075103352
i = 5.05503853283036, i-1 = 4.11006720700818, Exponential = 0.944971325822186
i = 6.77397334440211, i-1 = 5.05503853283036, Exponential = 1.71893481157175
i = 8.03325406790781, i-1 = 6.77397334440211, Exponential = 1.2592807235057
i = 9.99797822010981, i-1 = 8.03325406790781, Exponential = 1.964724152202
i = 10.540051694898, i-1 = 9.99797822010981, Exponential = 0.542073474788196
i = 10.6332298644808, i-1 = 10.540051694898, Exponential = 0.0931781695828122
....
i = 1970.86834655692, i-1 = 1968.91989881306, Exponential = 1.94844774386271
i = 1971.49302600885, i-1 = 1970.86834655692, Exponential = 0.62467945192265
i = 1972.16711634654, i-1 = 1971.49302600885, Exponential = 0.674090337697884
i = 1974.5740025773, i-1 = 1972.16711634654, Exponential = 2.40688623075635
i = 1978.14531015105, i-1 = 1974.5740025773, Exponential = 3.5713075737529
i = 1979.15315663014, i-1 = 1978.14531015105, Exponential = 1.00784647908321

这里的数学看起来非常合适。


旁注:您可以将所有额外的方法(ExponentialUniform等)声明为static,这样您就不必创建新的Program只是为了使用它们。

答案 2 :(得分:0)

你没有设置到达[0]的值,它没有在for循环之前初始化,所以数组中的其他值计算错误。

答案 3 :(得分:0)

额外建议,

public double Uniform(long a, long b)
{
    double c = ((double)a + ((double)b - (double)a) * Random());
    return c;
}

改变你的统一功能。