c++多种情况不同概率下,在范围内使用随机数

多种情况不同概率下,在范围内使用随机数。比如有5种情况,每种情况的概率不相同,有的概率为25,有的为15.

使用random库的函数来生成随机数。

cpp 复制代码
std::random_device rd;  //产生随机数种子
std::mt19937 gen(rd()); //生成伪随机数,使用rd作为种子
std::uniform_real_distribution<> dist(0, total_weight);  //生成指定范围均匀分布的实数
int random_number = dist(gen);  //调用均匀实数分布生成一个随机数

在五种概率情况下,通过生成随机数从五类中抽取一个随机结果。完整代码如下:

cpp 复制代码
#include <iostream>
#include <random>
#include <vector>


enum EMoonCakePieceType {
    E_PIECE_FLY = 0,    // 碎片1
    E_PIECE_LU = 1,    // 碎片2
    E_PIECE_CROSS = 2,    // 碎片3
    E_PIECE_MIDDLE = 3,    // 碎片4
    E_PIECE_AUTUMN = 4,    // 碎片5
};

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());  // 设置种子

    // 每个字的概率
    std::vector<std::pair<int, int>> words = {{E_PIECE_FLY,    15},
                                              {E_PIECE_LU,     15},
                                              {E_PIECE_CROSS,  25},
                                              {E_PIECE_MIDDLE, 15},
                                              {E_PIECE_AUTUMN, 30}};
    // 计算总权重
    int total_weight = 0;
    for (std::vector<std::pair<int, int>>::const_iterator it = words.begin(); it != words.end(); ++it) {
        total_weight += it->second;
    }
    std::vector<int> chinese;
    std::uniform_real_distribution<> dist(0, total_weight);
    int random_number = dist(gen);

    int result_number = 4;
    int cumulative_weight = 0;
    for (std::vector<std::pair<int, int>>::iterator itb = words.begin(); itb != words.end(); ++itb) {
        cumulative_weight += itb->second;
        if (random_number <= cumulative_weight) {
            result_number = itb->first;
            break;
        }
    }
    std::cout << result_number << std::endl;
    chinese.push_back(result_number);
    std::cout << "生成了:";
    for (auto word: chinese) {
        std::cout << word << " ";
    }  // 测试结果
    std::cout << std::endl;

    return 0;
}

开启个while循环测试多次运行,代码运行结果如下:

cpp 复制代码
#include <iostream>
#include <random>
#include <vector>
#include <thread>


enum EMoonCakePieceType {
    E_PIECE_FLY = 0,    // 碎片1
    E_PIECE_LU = 1,    // 碎片2
    E_PIECE_CROSS = 2,    // 碎片3
    E_PIECE_MIDDLE = 3,    // 碎片4
    E_PIECE_AUTUMN = 4,    // 碎片5
};

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());  // 设置种子

    // 每个字的概率
    std::vector<std::pair<int, int>> words = {{E_PIECE_FLY,    15},
                                              {E_PIECE_LU,     15},
                                              {E_PIECE_CROSS,  25},
                                              {E_PIECE_MIDDLE, 15},
                                              {E_PIECE_AUTUMN, 30}};
    // 计算总权重
    int total_weight = 0;
    for (std::vector<std::pair<int, int>>::const_iterator it = words.begin(); it != words.end(); ++it) {
        total_weight += it->second;
    }
    std::vector<int> chinese;
    while (true) {
        std::uniform_real_distribution<> dist(0, total_weight);
        int random_number = dist(gen);

        int result_number = 4;
        int cumulative_weight = 0;
        for (std::vector<std::pair<int, int>>::iterator itb = words.begin(); itb != words.end(); ++itb) {
            cumulative_weight += itb->second;
            if (random_number <= cumulative_weight) {
                result_number = itb->first;
                break;
            }
        }
        std::cout << result_number << std::endl;
        chinese.push_back(result_number);
        std::cout << "生成了:";
        for (auto word: chinese) {
            std::cout << word << " ";
        }  // 测试结果
        std::cout << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }
    return 0;
}

代码运行结果示例如下:

相关推荐
朱嘉鼎1 小时前
C语言之可变参函数
c语言·开发语言
SunkingYang1 小时前
详细介绍C++中捕获异常类型的方式有哪些,分别用于哪些情形,哪些异常捕获可用于通过OLE操作excel异常
c++·excel·mfc·异常捕获·comerror
Han.miracle3 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
北冥湖畔的燕雀4 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
mit6.8245 小时前
前后缀分解
算法
QX_hao5 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
你好,我叫C小白5 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Evand J7 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
Larry_Yanan8 小时前
QML学习笔记(四十二)QML的MessageDialog
c++·笔记·qt·学习·ui
寂静山林8 小时前
UVa 10228 A Star not a Tree?
算法