-2

我有个问题。“每分钟有 5 到 10 名顾客随机到达结账。” 我需要根据他们到达时间的顺序将这些客户放在一个列表中。

我不知道如何每分钟生成随机数量的客户,也不知道如何假设他们的到达时间。

这是使用 c++ 代码和使用 threadtest.c 代码的 Nachos 服务器

4

1 回答 1

1

使用毒药分布。这将创建一个由 3600 个整数组成的向量,每个整数都包含在那一秒内进来的客户数量。向量的索引是时间戳。

请记住,在任何给定的一秒内都会有超过 1 个客户进来。如果是这样,每个客户将具有相同的时间戳。根据需要更改时间分辨率。您也可以只创建一个包含时间和 customer_count 的元组向量,然后推送一个条目,只有一个或多个客户在任何给定秒内到达。那是你的任务。这是为了说明泊松分布。

#include <iostream>
#include <random>
#include <vector>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::poisson_distribution<> d(5/60.0);  // set average for Poisson distribution for 5 per minute

    std::vector<int> customers_at_each_second;
    int total_customers = 0;
    for (int i = 0; i < 3600; i++)
    {
        int customers_at_this_second = d(gen);
        customers_at_each_second.push_back(customers_at_this_second);
        total_customers += customers_at_this_second;
    }
    std::cout << "Total Customers in the hour " << total_customers << '\n';
}
于 2019-09-08T04:06:48.897 回答