-1

我为一个班级编写的这个程序使用“随机”数字来生成工作的到达(到达[i])和服务时间(服务[i])。我目前的问题是到达时间。为了获得到达时间,我调用了一个名为指数的函数,并将返回值添加到数组中的前一个到达时间 (arrival[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

4 回答 4

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

这里的数学对我来说看起来完全正确。


旁注:您可以将所有额外的方法( 、 等)声明ExponentialUniformstatic因此您不必Program为了使用它们而创建新方法。

于 2012-11-06T15:43:01.467 回答
1

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

我并不惊讶您在到达数组中的值往往很大。我实际上希望他们这样做。

另外:尝试根据它们的作用命名您的方法。您的Exponential方法与数学指数无关。

并且尽量不要自己实现随机数生成器。使用Random.Net Framework 中包含的类。如果您希望始终拥有相同的伪随机数序列(如您所愿),您可以使用常量对其进行播种。

于 2012-11-06T15:40:17.010 回答
0

额外的建议,

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

像这样改变你的统一功能。

于 2012-11-06T15:40:47.410 回答
0

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

于 2012-11-06T15:31:53.777 回答