c - 餐厅模拟

标签 c simulation

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define Empty 1
#define Full 0

float ChanceOfArrival (float CustomersPerMinute)
{
float CustomersArrivalChance;
int i;

/*Customers arriving per second */

CustomersArrivalChance = (CustomersPerMinute / 60) * 100;

printf ("The chance of customers arriving is: %0.3f%%\n", CustomersArrivalChance);

/* each 10 minute interval */

for (i = 0; i <= 18; i++)
{
    intervals (CustomersArrivalChance);
}

return CustomersArrivalChance;
}

int intervals (CustomersArrivalChance)
{

int totalCustomers = 0, totalWait = 0, queue = 0, SecondsInterval, waitingCustomers = 0;
int Cash1Salad, Cash1Buger, Cash2Salad, Cash2Burger;
int Cash1 = 1, Cash2 = 1;
int cointoss;
int x, Empty1, Empty2;
int CustomersServed = 0;
float RatePerMinute = 0, AverageWait = 0;
static int intervalNumber;
srand(time(NULL));

/*What could possibly happen every second in a 10 minute interval */
for (SecondsInterval = 0; SecondsInterval <= 600; SecondsInterval++)
{
    x = rand() % 101;

    if (CustomersArrivalChance >= x)
    {
        /*Customers Arrive this second */

        totalCustomers++;
        queue++;
        /*Choosing a cash at random */

        cointoss = rand()%2;
     if (queue > 0)
     {



        /* Cash 1 is open cash 2 is busy so the customer goes to cash 1 and chooses
        at random what they want to eat */
        if ((Cash1 == Empty) && (Cash2 != Empty) || (cointoss == 1) )
        {
            Cash1 = Full;
            queue--;

            switch ((rand()%2))
            {
                case 0:
                    Cash1Salad = rand()% 66 + 55;
                    totalWait = totalWait + Cash1Salad;
                    Empty1 = Cash1Salad;
                    CustomersServed++;
                    break;
                case 1:
                    Cash1Buger = rand()% 130 + 111;
                    totalWait = totalWait + Cash1Buger;
                    Empty1 = Cash2Burger;
                    CustomersServed++;
                    break;
            }
        }

        /* Cash 1 is busy cash 2 is open customer goes to cash 2 and chooses what they want */
        else if (Cash2 = Empty)
        {
            Cash2 = Full;
            queue--;

            switch ((rand()%2))
            {
                case 0:
                    Cash2Salad = rand()% 75 + 65;
                    totalWait = totalWait + Cash2Salad;
                    Empty2 = Cash2Salad;
                    CustomersServed++;
                    break;
                case 1:
                    Cash2Burger = rand()% 140 + 121;
                    totalWait = totalWait + Cash2Burger;
                    Empty2 = Cash2Burger;
                    CustomersServed++;
                    break;
            }
        }

        /*Both cashes are busy so the customer has to wait until one cash opens */
        else
        {
            totalWait++;
            waitingCustomers++;

        }

        /*Clearing Cash 1 if someone went there */
        if (Empty1 > 0)
        {
            Empty1--;
        }
        /*empty1 is equal to 0 then cash 1 is empty */
        else
        {
            Cash1 = Empty;
        }
        /*Clearing cash 2 is someone went there */
        if (Empty2 > 0)
        {
            Empty2--;
        }
        /*empty2 is equal to 0 then cash 2 is empty */
        else
        {
            Cash2 = Empty;
        }
     }
    }
    else
    {
        /*nothing happens because no customer showed up */
    }

}

intervalNumber++;

AverageWait = ((totalWait*1.0)/ (totalCustomers));
printf ("The average waiting time per customer in seconds is %0.2f in the interval      %d\n\n", AverageWait, intervalNumber);

printf  ("The total customers that arrived in the interval %d is %d\n\n", intervalNumber, totalCustomers);

}


int main (void)
{
    float CustomersPerMinute;

    printf ("Enter in the number of customers you want to arrive per minute:\n");
    scanf ("%f", &CustomersPerMinute);

    ChanceOfArrival(CustomersPerMinute);


    return 0;
}

你好,我有这个程序,它假设模拟一家只供应沙拉或汉堡的餐厅,只有两个收银员,只有一个队列供顾客排队等候服务。

我不确定为什么它不起作用,但我相信一切都合乎逻辑,但是当我运行这个程序时,它只是打印出预期的平均等待时间。

但问题是打印的平均等待时间在每个时间间隔内都是相同的。这不是我想要的。

从逻辑上讲,我认为每个时间间隔的平均等待时间应该不同,因为客户是随机生成的。然而,情况并非如此,因为平均等待时间始终相同,我该如何解决这个问题?

此外,每个时间间隔的客户总数相同,但由于客户是随机生成的,所以应该不同,我该如何解决这个问题?

最佳答案

看起来你在循环 srand。因此,它可能会在给定的一秒内为您提供相同的 rand() 结果。 不过 Stack overflow 不是调试论坛 ;)。

关于c - 餐厅模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20223703/

相关文章:

c++ - 变量名称中物理单位的样式?

c++ - 在另一个文件中访问 malloc 的函数

c++ - 为什么在设置 FPS 时在 SDL 中启动计时器?

c# - 有关人体模拟器设计的资源。 (想想13楼)

java - 二维绳索模拟

c - 改变 C 指针的值后释放它

c++ - 在c(或c++)中使用rb_define_singleton_method

c++ - 具有多个 SystemC 模拟的项目导致异常

methods - 在其他游戏中找到最佳的游戏策略,例如在纸牌游戏Toepen中

尽管有随机组件,但当我多次调用一个函数时,Python 给出相同的结果