易采站长站为您分析C++抽奖程序实现方法,实例分析了C++随机数的生成技巧与抽奖程序的实现方法,需要的朋友可以参考下
本文实例讲述了C++抽奖程序实现方法。。具体实现方法如下:
一、int rand()可以生成从[0, 65536)之间均匀分布的随机数。
现要求实现:有30万员工,使用rand()写一个抽奖程序,抽出人100获奖。
- #include <iostream> #include <set>
- using namespace std; typedef set<int> ISET;
- ISET GetPridePersonId(const int num, const int pride_num) {
- int id; ISET iset;
- while (1) { id = (int)((double)rand() / RAND_MAX * num) % (num - 1);
- if (iset.find(id) == iset.end()) { iset.insert(id);
- } if (iset.size() >= pride_num) {
- break; }
- } return iset;
- } void print(ISET &iset)
- { ISET::iterator iter;
- cout<<"item as :n"; for (iter = iset.begin(); iter != iset.end(); ++ iter) {
- cout<<*iter<<"n"; }
- } int main(int argc, char **argv)
- { const int total_person = 300000;
- const int total_pride_person = 100; ISET iset = GetPridePersonId(total_person, total_pride_person);










