利用随机数实现指定概率抽奖

一、随机数与概率的规律

假设我们使用随机数生成器,可以产生1-100范围内随机数。

那么每次产生的随机数,其值可能是1-100范围内任意一个数,每个数的概率均等。

所以可以得出,随机数值V与概率P,有如下规律:

数值(V) 概率(P)
1 <= V <= 100 100%
V < 1 或 V > 100 0%
1 <= V <= 50 50%
50 <= V <= 100 50%
1 <= V <= 20 20%

二、利用随机数设计一个抽奖程序

假设可以进行无限次抽奖,奖项有一、二、三、四等奖、谢谢参与,如下:

// 奖项
enum Prize
{
    First,     // 一等奖
    Second,    // 二等奖
    Third,     // 三等奖
    Fourth,    // 四等奖
    Thanks     // 谢谢参与
};

各奖项抽中的概率为:

奖项 概率
一等奖 5%
二等奖 10%
三等奖 20%
四等奖 30%
谢谢参与 35%

首先,封装PrizeControl类,实现抽奖逻辑。

PrizeControl.h

#ifndef PRIZECONTROL_H
#define PRIZECONTROL_H

// 奖项
enum Prize
{
    First,     // 一等奖
    Second,    // 二等奖
    Third,     // 三等奖
    Fourth,    // 四等奖
    Thanks     // 谢谢参与
};

// 抽奖控制类
class PrizeControl
{

public:
    PrizeControl();

    // 抽奖,返回抽奖结果
    Prize drawLottery();
};

#endif // PRIZECONTROL_H

PrizeControl.cpp

#include "PrizeControl.h"
#include <time.h>
#include <stdlib.h>

PrizeControl::PrizeControl()
{
    srand(time(nullptr)); // 设置随机数种子
}

Prize PrizeControl::drawLottery()
{
    // 生成一个1 ~ 100范围内的随机数
    int value = rand() % 100 + 1// 1 ~ 100

    if (value >= 1 && value <= 5)
    {
        return First; // 一等奖5%
    }
    else if (value > 5 && value <= 15)
    {
        return Second; // 二等奖10%
    }
    else if (value > 15 && value <= 35)
    {
        return Third; // 三等奖20%
    }
    else if (value > 35 && value <= 65)
    {
        return Fourth; // 四等奖30%
    }
    else
    {
        return Thanks; // 谢谢参与35%
    }
}

drawLottery()中对生成随机数所在区域进行判断,并返回相应奖项,比较简单,就不多说啦。

然后,在main.cpp中进行测试,如下:

#include <QCoreApplication>
#include <QDebug>
#include "PrizeControl.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int firstCount = 0;     // 一等奖抽中次数
    int secondCount = 0;    // 二等奖抽中次数
    int thirdCount = 0;     // 三等奖抽中次数
    int fourthCount = 0;    // 四等奖抽中次数
    int thanksCount = 0;    // 谢谢参与抽中次数

    PrizeControl prizeControl;
    for (int i = 0; i < 2000; i++) // 总共抽奖次数2000
    {
        Prize prize = prizeControl.drawLottery();
        switch (prize)
        {
        case First:
            firstCount++;
            break;
        case Second:
            secondCount++;
            break;
        case Third:
            thirdCount++;
            break;
        case Fourth:
            fourthCount++;
            break;
        default:
            thanksCount++;
            break;
        }
    }

    qDebug() << "firstCount:" << firstCount;    //100
    qDebug() << "secondCount:" << secondCount;  //200
    qDebug() << "thirdCount:" << thirdCount;    //400
    qDebug() << "fourthCount:" << fourthCount;  //600
    qDebug() << "thanksCount:" << thanksCount;  //700

    return a.exec();
}

运行结果:

利用随机数实现指定概率抽奖
在这里插入图片描述

可以看到,共抽奖2000次,各奖项所抽中的比例基本上与原设定值相等。

三、题外话

开源项目diskspd中,github地址:https://github.com/microsoft/diskspd

在IORequestGenerator.cpp文件中,利用随机数实现IO读写请求比例的控制。

/*****************************************************************************/
// Decide the kind of IO to issue during a mix test
// Future Work: Add more types of distribution in addition to random
__inline static IOOperation DecideIo(Random *pRand, UINT32 ulWriteRatio)
{
    return ((pRand->Rand32() % 100 + 1) > ulWriteRatio) ? IOOperation::ReadIO : IOOperation::WriteIO;
}

比如ulWriteRatio=30,则表示生成的IO请求中,IO写占30%,IO读占70%,继而实现了混合读写时,读写IO请求比例的控制。


本文涉及工程代码,公众号回复:54Random,即可下载。


本篇文章来源于微信公众号: 超哥学编程

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/10831.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!