选择两个城市的概率c++

Probability of choosing two cities C++

本文关键字:城市 概率 c++ 两个 选择      更新时间:2023-10-16

我正在编写一个程序,其中我有一个包含34个城市的列表,我想给每个城市一个被选中的概率。

所以我有:

vector<float> vec;
int s;
    cin >> s;
    srand(s);
    for (int k=0; k < 34; k++)
    {
    float p1= (float)rand()/(float)((unsigned)RAND_MAX+1);
    vec.push_back(p1);
    }

给每个城市一个概率。我现在的问题是,我想做一个随机数生成器,从这些城市中选择两个。例如city1有5%,city2有2%,city3有3%,等等。我如何根据给定的概率随机选择其中两个城市?

我是用遗传算法做的。对于你的城市,考虑一条10个单位的线。现在从0-5个单位在线是city1 6-7的city2和8-9的city3。现在从0-9中随机选择一个数字。并找出它在哪个城市范围内

乍一看,我的解决方案是:

  • 创建一个等于所有城市概率的数字
  • 创建一个随机数,最大随机数等于前一个随机数

  • 取随机数,并通过您的城市矢量并取对应的那个

的例子:

City 1 : 5%
City 2 : 8%
City 3 : 2%
City 4 : 5%
City 5 : 12%
Create a number -> Number a = 32 (5+8+2+5+12)
Generate a number with : 1 <= number
Assume that the number is equal to 12
City 1 is choose if : 1 <= number <= 5 (So not)
City 2 is choose if : 6 <= number <= 13 (So yes)
City 2 is choose.

如果你有任何问题,欢迎提问:)

编辑:

好吧,我会给你更多的解释。

用下面的代码:

for (int k=0; k < 10; k++)
{
    float p1= (float)rand()/(float)((unsigned)RAND_MAX+1);
    vec.push_back(p1);
}

现在假设vec包含以下信息:

5
3
8
5
12
14
8
5
6
18

用每个数字对应选择一个城市的概率。

5 -> 5% probability to choose (City1)
3 -> 3% probability to choose (City2)
8 -> 8% probability to choose (City3)
... etc

现在我将给你一些代码,我将解释它:

int nbReference = 0;
for (auto it = vec.begin() ; it != vec.end() ; ++it)
{
   nbReference += *it;
}
nbReference = rand(nbReference);
int i = 0;
int cityProbability = 0;
for (auto it = vec.begin() ; it != vec.end() ; ++it)
{
   cityProbability = *it;
   if ((i + cityProbability) > nbReference)
   {
      return (i + 1);
   }
   i += cityProbability;
}

首先,我创建一个等于所有城市概率之和的数字

   int nbReference = 0;
    for (auto it = vec.begin() ; it != vec.end() ; ++it)
    {
       nbReference += *it;
    }

其次,生成一个符合以下范围的数字-> 0

第三,我创建一个循环,一个接一个地获取所有城市,当我们找到正确的城市时退出。

我们如何知道一个城市是好的?

让我们举个例子!

与我们之前的概率

5 3 8 5 12 14 8 5 6 18

NbReference =(5+3+8+5+12+14+8+5+6+18)所以84

对于每个城市,我们将设置一个等于他的概率加上所有前一个城市的概率的范围。我给你示范一下:

    5 -> Range 0 to 4  (0 to 4 = 5 ---> 5%)
    3 -> Range 5 to 8  (5 to 8 = 3 ---> 3%)
    8 -> Range 9 to 17 
    5 -> Range 18 to 22
    ... etc

如果这里创建的数字

nbReference = rand(nbReference);

在城市范围内,因此选择该城市。

示例:如果数字是16,则选择city3 !

    5 -> Range 0 to 4  Number is 16 so NOPE
    3 -> Range 5 to 8  Number is 16 so NOPE
    8 -> Range 9 to 17 Number is 16 so YES!
    5 -> Range 18 to 22
    ... etc

这有帮助吗?:)

有什么问题吗?不客气

也许这段代码可以帮助你(部分遵循WhozCraig的建议)

#include <iostream>
#include <random>
#include <algorithm>

int main(int argc, const char * argv[])
{
    using namespace std::chrono;
    system_clock::time_point tp = system_clock::now();
    system_clock::duration dtn = tp.time_since_epoch();
    std::default_random_engine generator(static_cast<int>(dtn.count()));
    //Generate 34 cities
    std::uniform_real_distribution<double> gen_distribution(0,1);
    auto getProb = std::bind ( gen_distribution, generator );
    std::vector<double> citiesProb;
    double probSum(0.0);
    double cityProb(0.0);
    for (int k=0; k < 34; k++)
    {
        cityProb = getProb();
        probSum += cityProb;
        citiesProb.push_back(cityProb);
    }
    //Pick 7 cities
    std::uniform_real_distribution<double> pick_distribution(0,probSum);
    auto pickCity = std::bind ( pick_distribution, generator );
    double chooseCity;
    double probBasket;
    for (int k=0; k < 7; ++k)
    {
        probBasket = 0.0;
        chooseCity = pickCity();
        for (int i = 0; i < citiesProb.size(); ++i)
        {
            if (chooseCity >= probBasket && chooseCity < probBasket + citiesProb[i])
            {
                std::cout << "City with index " << i << " picked" << std::endl;
            }
            probBasket += citiesProb[i];
        }
    }
    return 0;
}

工作原理:

city1 5%(0.05), city2 25%(0.25), city3 8%(0.08), city4 10%(0.1)
然后

probSum = 0.05 + 0.25 + 0.08 + 0.1 = 0.48

然后选择0到0.48之间的数字(命名为pickProb)和

if pickProb is between 0 and 0.05 pick city1 (prob = 0.05/0.48 = 10%)
if pickProb is between 0.05 and 0.30 pick city2 (prob = 0.25/0.48 = 52%)
if pickProb is between 0.30 and 0.38 pick city3 (prob = 0.08/0.48 = 16%)
if pickProb is between 0.38 and 0.48 pick city4 (prob = 0.1/0.48 = 20%)

如果probSum = 1.0,则city1被选中的概率为5%,city2被选中的概率为25%,以此类推。