我要生成一个正态分布的伪随机数数组。据我所知,std库为此提供了以下代码:
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> d(mean,std);
...
double number = d(gen);问题是我想使用Sobol的准随机序列,而不是Mersenne伪随机发生器。因此,我的问题是:是否可以使用用户定义的随机生成器(在我的例子中使用Sobol的准随机序列生成器)运行std::normal_distribution?
更多细节:我有一个名为RandomGenerators的类,用于生成Sobol的准随机数:
RandomGenerator randgen;
double number = randgen.sobol(0,1);发布于 2013-10-04 10:17:19
是的,这是可能的。只需使其符合统一随机数发生器的要求(第26.5.1.3节第2和第3款):
2如果表116所示的表达式有效且具有指定的语义,且
G类满足统一随机数生成器的要求,且G也满足本节的所有其他要求。在该表中并贯穿本节: ( a)G’s associatedresult_type, and b)gis a value ofG. 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()`命名的类型是T。
发布于 2017-01-08 16:52:22
这里要提醒一下--我在实现这个程序时遇到了一个很大的问题。如果max()、/min()、/operator()的返回类型不是64位,那么发行版将重采样。我的(无符号) 32位Sobol实现每偏差采样两次,从而破坏了数字的属性。此代码复制:
#include <random>
#include <limits>
#include <iostream>
#include <cstdint>
typedef uint32_t rng_int_t;
int requested = 0;
int sampled = 0;
struct Quasi
{
rng_int_t operator()()
{
++sampled;
return 0;
}
rng_int_t min() const
{
return 0;
}
rng_int_t max() const
{
return std::numeric_limits<rng_int_t>::max();
}
};
int main()
{
std::uniform_real_distribution<double> dist(0.0,1.0);
Quasi q;
double total = 0.0;
for (size_t i = 0; i < 10; ++i)
{
dist(q);
++requested;
}
std::cout << "requested: " << requested << std::endl;
std::cout << "sampled: " << sampled << std::endl;
}输出(使用g++ 5.4):
requested: 10
sampled: 20甚至在用-m32编译时。如果您将rng_int_t更改为64位,问题就会消失。我的解决办法是将32位值放入返回值的最重要位中,例如
return uint64_t(val) << 32;发布于 2019-05-22 07:47:27
现在可以直接用Boost生成Sobol序列。见boost/随机/sobol.hpp。
https://stackoverflow.com/questions/19178647
复制相似问题