Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何实施抽奖制度,使奖品具有相对获得机会的比率?_C++ - Fatal编程技术网

C++ 如何实施抽奖制度,使奖品具有相对获得机会的比率?

C++ 如何实施抽奖制度,使奖品具有相对获得机会的比率?,c++,C++,例如,我有一个奖品列表,对于每个奖品,一个相对比率常数表示获得奖品的机会,如下代码所示: vector<pair<string,int> > prizeBox; prizeBox.push_back(make_pair("toy car",100)); prizeBox.push_back(make_pair("football",50)); prizeBox.push_back(make_pair("book",50)); prizeBox.push_back(make

例如,我有一个奖品列表,对于每个奖品,一个相对比率常数表示获得奖品的机会,如下代码所示:

vector<pair<string,int> > prizeBox;
prizeBox.push_back(make_pair("toy car",100));
prizeBox.push_back(make_pair("football",50));
prizeBox.push_back(make_pair("book",50));
prizeBox.push_back(make_pair("cash 1000",10));
prizeBox.push_back(make_pair("cash 5000",5));
prizeBox.push_back(make_pair("free expensive lunch",2));
prizeBox.push_back(make_pair("free expensive dinner",2));
prizeBox.push_back(make_pair("special big price",1));
vectorPrizebox;
prizeBox.向后推(做一对(“玩具车”,100));
prizeBox.向后推(做一对(“足球”),50);
prizeBox.向后推(做一对(“书”,50));
prizeBox.推回(制作成对(“现金1000”,10));
prizeBox.向后推(制作成对(“现金5000”,5));
prizeBox.push_back(制作双(“免费昂贵的午餐”),2);
prizeBox.向后推(制作双(“免费昂贵的晚餐”,2));
prizeBox.push_-back(制作双(“特价大减价”,1));
在上述代码中,“玩具车”有100/(100+50+50+10+5+2+2+1)的机会获得。我知道rand()可以用来得到一个随机数,但在这种情况下我该如何使用它呢


(我也不认为我需要一个新的向量来加上100个“玩具车”,50个“足球”等等…

如果你想用更高的概率来索引,我想我会这样做

srand(time(NULL));

vector<pair<string,int> > prizeBox; 

int total = 100+50+50+10+5+2+2+1;

int random = rand()%(total);
if(random < 100){
    // acces to prizeBox[0];
}
else if(random < 150){
    //acces to prizeBox[1];
}
else if (random < 200)
    // and so on 
srand(时间(空));
矢量prizeBox;
整数合计=100+50+50+10+5+2+2+1;
int random=rand()%(总计);
如果(随机<100){
//访问prizeBox[0];
}
否则如果(随机<150){
//访问prizeBox[1];
}
否则如果(随机<200)
//等等
PS:您可以使用初始值设定项列表来初始化您的选项卡,方法是执行以下操作(这比几次向后推要漂亮)

    vector<pair<string,int> > prizeBox  = { {"toy car",100} , {"football",50} };
vector prizeBox={{“玩具车”,100},{“足球”,50};

您可以轻松使用
std::discrete_distribution
而不是创建自己的发行版:

int main()
{
    //create vector and fill it
    std::vector<std::tuple<std::string,int> > prizeBox { {"toy car",100}, {"football",50}, {"book",50}, {"cash 1000",10} };

    //create a vector of the second entries of vector-elements of prizeBox
    std::vector<int> chance;
    std::transform(std::begin(prizeBox), std::end(prizeBox), std::back_inserter(chance), [](auto p){return std::get<1>(p);});

    //set up the distribution and the random number generator
    auto dist = std::discrete_distribution<int>(std::begin(chance), std::end(chance));
    std::mt19937_64 rng;

    //draws ten time from the prize box
    for(int i=0;i<10;++i)
    {
        std::cout<<std::get<0>(prizeBox[dist(rng)])<<std::endl;
    }
}
intmain()
{
//创建向量并填充它
标准:矢量prizeBox{{“玩具车”,100},{“足球”,50},{“书”,50},{“现金1000”,10};
//创建prizeBox向量元素第二个条目的向量
性病媒机会;
std::transform(std::begin(prizeBox),std::end(prizeBox),std::back_插入器(chance),[](autop){return std::get(p);});
//设置分布和随机数生成器
自动距离=标准::离散分布(标准::开始(机会),标准::结束(机会));
标准:mt19937\U 64 rng;
//从奖品盒中抽出十次

对于(int i=0;i这里有一个函数,它根据你想要的机会抽取一个随机奖。其思想是首先通过向量进行迭代,得到机会之和。然后,从1到该和生成一个随机数,然后再次迭代,从随机数中减去当前机会。当随机数小于当前ch时ance,您已获得已排序的奖品。下面的代码显示了实现

#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

string drawPrize (vector<pair<string, int> > prizeBox) {
    //iterate through vector to get the sum of the weigths.
    int total = 0;
    for (vector<pair<string,int> >::iterator it = prizeBox.begin() ; it != prizeBox.end(); ++it)
        total += it->second;

    // getting a random number
    srand(time(NULL));
    int r = rand() % total + 1; // a number from 1 to total

    // finding the prize
    vector<pair<string,int> >::iterator it = prizeBox.begin();
    while (r > it->second && it != prizeBox.end()) {
        r -= it->second;
        it++;
    }

    // returning the sorted prize
    return it->first;
}
#包括
#包括
#包括
#包括
使用名称空间std;
弦乐奖(矢量prizeBox){
//在向量中迭代以获得权重之和。
int-total=0;
for(vector::iterator it=prizeBox.begin();it!=prizeBox.end();++it)
总计+=它->秒;
//获取随机数
srand(时间(空));
int r=rand()%total+1;//从1到total的数字
//寻找奖品
向量::迭代器it=prizeBox.begin();
而(r>it->second&&it!=prizeBox.end()){
r-=它->秒;
it++;
}
//退回已分类的奖品
返回->第一个;
}