【问题标题】:Is there a function to generate random number using triangular distribution in c++?是否有使用c ++中的三角分布生成随机数的函数?
【发布时间】:2019-04-15 00:42:21
【问题描述】:

我在 c++ 中搜索一个函数,我给它( min , mode , max )然后它返回一个由三角分布生成的随机数。如果有实现这个功能的代码就好了。

【问题讨论】:

  • 也许std::piecewise_linear_distribution 可以满足您的需求?
  • 谢谢。但是我该如何使用? @miles budnek
  • R函数是什么意思?您指的是R 语言吗?您是否要求 C++ 中的函数类似于 R 中的函数?
  • @JosephWood 不,我使用 R 作为函数的名称。

标签: c++ simulation distribution


【解决方案1】:

std::piecewise_linear_distribution 可用于模拟三角分布。

这是一个基于链接的 cppreference 页面上的示例代码的示例,该示例代码生成一个三角形分布,该分布生成 0 到 30 之间的数字,峰值在 20:

#include <random>
#include <iostream>
#include <iomanip>
#include <array>
#include <map>

std::piecewise_linear_distribution<double> triangular_distribution(double min, double peak, double max)
{
    std::array<double, 3> i{min, peak, max};
    std::array<double, 3> w{0, 1, 0};
    return std::piecewise_linear_distribution<double>{i.begin(), i.end(), w.begin()};
}

int main() {
    std::random_device rd;
    // create a mersenne twister PRNG seeded from some implementation-defined random source
    std::mt19937 gen(rd());

    // create a triangular distribution with a minimum of 0, a peak at 20, and a maximum of 30
    auto dist = triangular_distribution(0, 20, 30);

    std::map<int, int> hist;

    // use our distribution to generate 10,000 random numbers
    // (truncated to integers for the sake of output; the generated numbers are actually real numbers)
    for (int i = 0; i < 10000; ++i) {
        double num = dist(gen);
        ++hist[num];
    }

    // print out a nice histogram of the numbers generated
    for(auto p : hist) {
        std::cout << std::setw(2) << std::setfill('0') << p.first << ' '
            << std::string(p.second/10,'*') << '\n';
    }
}

可能的输出:

00 **
01 *****
02 ******
03 ************
04 **************
05 ******************
06 **********************
07 *************************
08 **************************
09 *********************************
10 ************************************
11 **************************************
12 *************************************
13 ********************************************
14 **************************************************
15 **************************************************
16 *******************************************************
17 *******************************************************
18 ************************************************************
19 *****************************************************************
20 **************************************************************
21 *******************************************************
22 ************************************************
23 *******************************************
24 ***************************************
25 ******************************
26 **************************
27 ****************
28 ***********
29 ***

【讨论】:

    【解决方案2】:

    您可以通过对 U₁U₂ 这两个均匀分布求和来从 简单 三角形分布 T 中采样。在 C 语言中,这将是:

     float U1 = (float)rand() / RAND_MAX;
     float U2 = (float)rand() / RAND_MAX;
     float T  = U1 + U2;
    

    相应缩放:结果在 0 到 2 之间,众数为 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2011-11-10
      • 2018-06-22
      • 2020-03-28
      • 2019-02-28
      • 2011-04-14
      相关资源
      最近更新 更多