如何使用c ++生成不相关的随机序列 [英] how to generate uncorrelated random sequences using c++
问题描述
我想生成两个不相关的正态分布随机数X 1 ,X 2 序列.
由于正态分布随机数来自统一数,所以我需要的是两个不相关的统一序列.但是如何使用:
srand (time(NULL));
我想我需要播种两次或做类似的事情?
由于高质量随机数生成器生成的随机数是统一且独立的,因此您可以根据需要从中生成尽可能多的独立序列. /p>
您不需要,并且不应播种两个不同的生成器.
在C ++(11)中,应使用标题<random>
中的伪随机数生成器一个>.这是一个最小的示例,可以用作实际实施的模板:
std::random_device seed;
std::mt19937 gen{seed()};
std::normal_distribution<> dist1{mean1, sd1};
std::normal_distribution<> dist2{mean2, sd2};
现在,您可以通过调用dist1(gen)
和dist2(gen)
来生成独立的数字序列. random_device
用于播种实际的生成器,在我的代码中是Mersenne Twister生成器.这种类型的生成器高效且具有良好的统计特性.对于非加密安全的生成器,应将其视为默认选择.
I'd like to generate two sequences of uncorrelated normal distributed random numbers X1, X2.
As normal distributed random numbers come from uniform numbers, all I need is two uncorrelated uniform sequences. But how to do it using:
srand (time(NULL));
I guess I need to seed twice or do something similar?
Since the random numbers generated by a high-quality random-number generator are uniform and independent, you can generate as many independent sequences from it as you like.
You do not need, and should not seed two different generators.
In C++(11), you should use a pseudo-random number generator from the header <random>
. Here’s a minimal example that can serve as a template for an actual implementation:
std::random_device seed;
std::mt19937 gen{seed()};
std::normal_distribution<> dist1{mean1, sd1};
std::normal_distribution<> dist2{mean2, sd2};
Now you can generate independent sequences of numbers by calling dist1(gen)
and dist2(gen)
. The random_device
is used to seed the actual generator, which in my code is a Mersenne Twister generator. This type of generator is efficient and has good statistical properties. It should be considered the default choice for a (non cryptographically secure) generator.
这篇关于如何使用c ++生成不相关的随机序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!