代码示例
#include<iostream>
#include<cstdlib>
#include<ctime>
int main()
{
srand(static_cast<unsigned int>(time(nullptr))); // 设置随机数生成种子为当前时间
int total = 100000; // 假设有100000人参加抽奖,中奖人数为199人
int win = 199; // 中奖人数
int* winners = new int[win]; // 保存中奖者的序号
// 生成中奖者序号
for (int i = 0; i < win; ++i)
{
int idx = rand() % total;
winners[i] = idx;
}
// 输出中奖者的序号
std::cout << "Winners: ";
for (int i = 0; i < win; ++i)
{
std::cout << winners[i] << " ";
}
std::cout << std::endl;
delete[] winners;
return 0;
}
1 条评论
在畅想未来时需警惕乌托邦式理想化。