c++ - 使用用户定义的随机生成器运行 std::normal_distribution

标签 c++ visual-c++ c++11 random

我要生成一组正态分布的伪随机数。据我所知,std 库为此提供了以下代码:

std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> d(mean,std);
...
double number = d(gen);

问题是我想使用 Sobol 的准随机序列而不是 Mersenne Twister 伪随机生成器。所以,我的问题是: 是否可以使用用户定义的随机生成器(在我的例子中使用 Sobol 的准随机序列生成器)来运行 std::normal_distribution?


更多细节:我有一个名为 RandomGenerators 的类,用于生成 Sobol 的准随机数:

RandomGenerator randgen;
double number = randgen.sobol(0,1);

最佳答案

是的,这是可能的。只需使其符合统一随机数生成器的要求即可(§26.5.1.3 第 2 和第 3 段):

2 A class G satisfies the requirements of a uniform random number generator if the expressions shown in Table 116 are valid and have the indicated semantics, and if G also satisfies all other requirements of this section. In that Table and throughout this section:

a) T is the type named by G’s associatedresult_type`, and

b) g is a value of G.

Table 116 — Uniform random number generator requirements

Expression     | Return type | Pre/post-condition         | Complexity
----------------------------------------------------------------------
G::result_type |    T        | T is an unsigned integer   | compile-time
               |             | type (§3.9.1).             |
----------------------------------------------------------------------
g()            |    T        | Returns a value in the     | amortized constant
               |             | closed interval            |
               |             | [G::min(), G::max()].      |
----------------------------------------------------------------------
G::min()       |    T        | Denotes the least value    | compile-time
               |             | potentially returned by    |
               |             | operator().                |
----------------------------------------------------------------------
G::max()       |    T        | Denotes the greatest value | compile-time
               |             | potentially returned by    |
               |             | operator().                |

3 The following relation shall hold: G::min() < G::max().

关于c++ - 使用用户定义的随机生成器运行 std::normal_distribution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19178647/

相关文章:

c++ - 用 C++ 解释 boost::filesystem 的可移植通用路径格式

c++ - Visual Studio 2005 中的 Lambda 函数替代品——Boost 库

c++ - 如何构建自定义 libcurl 以仅支持 HTTP/HTTPS 协议(protocol)

c++ - 将 C++ vector 对写入文本文件

c++ - 是什么导致使用三元而不是短裤将这个有符号整型转换为无符号整型?

c++ - SFINAE 检查运算符+=

c++ - Visual C++ "List of Libraries"含义

windows - Windows 中 C/C++ 数据类型的区别

c++ - 如何使用 move 的对象?

c++ - 关于在模板类中声明友元模板函数的问题(C++)