抽奖实现

本文介绍了一个基于C++实现的抽奖程序,通过随机数生成算法来模拟不同奖项的概率分配,并记录每种奖项被抽中的次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 抽奖.cpp : 定义控制台应用程序的入口点。
//

include “stdafx.h”

include

include

include

using namespace std;

/*****************
中奖描述:
一等奖(A): 10 权重
二等奖(B): 20 权重
三等奖(C): 200 权重
四等奖(D): 800 权重

*********************/

enum EM_QuanZ
{
EM_A = 10,
EM_B = 20,
EM_C = 170,
EM_D = 800
};

int Bigrand()
{
//随机数列一般由srand()和rand()共同设置,从而得到真正意义上的随机
//设置随机种子
srand((unsigned)time(NULL));

return RAND_MAX*rand() + rand();

}

int a[4] = {0};

int GetRanDomKey()
{
//获取随机数
int number = (Bigrand() % (EM_A + EM_B + EM_C + EM_D)) + 1;

if ((number > 0) && (number <= EM_A))
{
    number = 1;
    ++a[0];
}
else if (number >= (EM_A + EM_B + EM_C))
{
    number = 4;
    ++a[3];
}
else if ((number >= (EM_A + EM_B)) && (number < (EM_A + EM_B + EM_C)))
{
    number = 3;
    ++a[2];
}
else if ((number >= EM_A) && (number < (EM_A + EM_B)))
{
    number = 2;
    ++a[1];
}
return number;

}
void GetAward(int key)
{
cout << “恭喜您中了” << key<<”等奖” << endl;
}

void Start()
{
while (true)
{
GetAward(GetRanDomKey());
int number = (rand() % 10) + 1;

    if ((a[0]+a[1]+a[2]+a[3]) == 100)
    {

        break;
    }
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}

}

int _tmain(int argc, _TCHAR* argv[])
{
std::thread td(Start);
td.join();

for (int i = 0; i < 4; i++)
{
    cout <<i+1 <<"等奖中奖次数: " << a[i] << endl;
}
system("pause");
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值