【问题标题】:C++: generate gaussian distributionC++:生成高斯分布
【发布时间】:2010-11-09 17:09:42
【问题描述】:

我想知道在 C++ 标准库中是否有任何 高斯分布 数字生成器,或者您是否有任何代码 sn-p 可以通过。

提前致谢。

【问题讨论】:

标签: c++ gaussian normal-distribution


【解决方案1】:

这个问题的答案随着 C​​++11 的变化而改变,它的 random header 包括 std::normal_distribution。 Walter Brown 的论文N3551, Random Number Generation in C++11 可能是对这个库的更好介绍之一。

以下代码演示了如何使用此标头 (see it live):

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

int main()
{
    std::random_device rd;

    std::mt19937 e2(rd());

    std::normal_distribution<> dist(2, 2);

    std::map<int, int> hist;
    for (int n = 0; n < 10000; ++n) {
        ++hist[std::floor(dist(e2))];
    }

    for (auto p : hist) {
        std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';
    }
}

在我对C++ random float number generation 的回答中,我提供了一组更通用的 C++11 随机数生成示例,其中包含 Boost 中的示例并使用了rand()

【讨论】:

    【解决方案2】:

    C++ 技术报告 1 增加了对随机数生成的支持。因此,如果您使用的是相对较新的编译器(visual c++ 2008 GCC 4.3),那么它很可能是开箱即用的。

    请参阅 here 以了解 std::tr1::normal_distribution(以及更多)的示例用法。

    【讨论】:

    【解决方案3】:

    GNU 科学图书馆具有此功能。 GSL - Gaussian Distribution

    【讨论】:

    • 大声笑,我在查找答案之前写了这个......我想我应该改变它:)
    【解决方案4】:

    标准库没有。但是,Boost.Random 可以。如果我是你,我会使用它。

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 2014-11-01
      • 2010-12-13
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 2012-09-12
      相关资源
      最近更新 更多