直接上代码了
#include <iostream>
#include <time.h>
#define RAND_MAX 0x7fff
using namespace std;
//抽奖结果数据结构
struct DrawReslut
{
int gift_ID;
int gift_type;
int gift_count;
};
//抽奖随机相关内容
int L_Rand(int begin, int end)
{
time_t tt = time(NULL);
srand(tt);
if (begin > end)
{
int temp = begin;
begin = end;
end = temp;
}
int seed = (int)((double)rand() / RAND_MAX * 10000000);
return seed % (end - begin + 1) + begin;
}
//抽奖执行
bool DrawOnce(DrawReslut& drawinfo)
{
int nRand = L_Rand(0, 9);
drawinfo.gift_ID = nRand;
drawinfo.gift_count = 0;
drawinfo.gift_type = 0;
return true;
}
int main()
{
DrawReslut res;
DrawOnce(res);
cout << "index" << res.gift_ID << endl;
return 0;
}
该方法在作为服务器抽奖有1到9概率一样,下一篇再写一个从配置文件读取概率表,然后再随机抽检