简体   繁体   English

在c ++ 11中使用一个随机引擎进行多分布

[英]using one random engine for multi distributions in c++11

I am using c++11 new <random> header in my application and in one class in different methods I need different random number with different distributions. 我在我的应用程序中使用c ++ 11 new <random>标头,并且在不同方法的一个类中,我需要具有不同分布的不同随机数。 I just put a random engine std::default_random_engine as class member seed it in the class constructor with std::random_device and use it for different distributions in my methods. 我只是将一个随机引擎std::default_random_engine作为类成员将它放在带有std::random_device的类构造函数中,并将其用于我的方法中的不同发行版。 Is that OK to use the random engine in this way or I should declare different engines for every distribution I use. 以这种方式使用随机引擎是可以的,或者我应该为我使用的每个分发声明不同的引擎。

It's ok. 没关系。

Reasons to not share the generator: 不共享发电机的原因:

  • threading (standard RNG implementations are not thread safe) 线程化(标准RNG实现不是线程安全的)
  • determinism of random sequences: 随机序列的确定性:

    If you wish to be able (for testing/bug hunting) to control the exact sequences generated, you will by likely have fewer troubles by isolating the RNGs used, especially when not all RNGs consumption is deterministic. 如果您希望能够(用于测试/追踪错误)来控制生成的确切序列,那么通过隔离所使用的RNG可能会减少麻烦,特别是当并非所有RNG消耗都是确定性的时候。

You should be careful when using one pseudo random number generator for different random variables, because in doing so they become correlated. 将一个伪随机数生成器用于不同的随机变量时应该小心,因为这样做会使它们变得相关。

Here is an example: If you want to simulate Brownian motion in two dimensions (eg x and y) you need randomness in both dimensions. 下面是一个示例:如果要在两个维度(例如x和y)中模拟布朗运动,则需要在两个维度上进行随机性。 If you take the random numbers from one generator (noise()) and assign them successively 如果从一个生成器(noise())获取随机数并连续分配它们

while(simulating)
    x = x + noise()
    y = y + noise()

then the variables x and y become correlated, because the algorithms of the pseudo number generators only make statements about how good they are, if you take every single number generated and not only every second one like in this example. 然后变量x和y变得相关,因为伪数生成器的算法只会生成它们有多好的陈述,如果你生成每一个生成的数字,而不是像本例中那样每隔一个生成一个数。 Here, the Brownian particles could maybe move into the positive x and y directions with a higher probability than in the negative directions and thus introduce an artificial drift. 这里,布朗粒子可能以比在负方向上更高的概率移动到正x和y方向,从而引入人工漂移。

For two further reasons to use different generators look at sehe's answer. 使用不同发电机的另外两个原因请看sehe的答案。

MosteM's answer isn't correct. MosteM的答案不正确。 It's correct to do this so long as you want the draws from the distributions to be independent. 只要您希望分布中的绘制是独立的,这样做是正确的。 If for some reason you need exactly the same random input into draws of different distributions, then you may want different RNGs. 如果由于某种原因,您需要完全相同的随机输入到不同分布的绘制,那么您可能需要不同的RNG。 If you want correlation between two random variables, it's better to build them starting from a common random variable using mathematical principal: eg, if A, B are independent normal(0,1), then A and aA +sqrt(1-a**2)B are normal(0,1) with correlation a. 如果你想要两个随机变量之间的相关性,最好从一个使用数学原理的公共随机变量开始构建它们:例如,如果A,B是独立的法线(0,1),那么A和aA + sqrt(1-a * * 2)B是正常的(0,1),相关性为a。

EDIT: I found a great resource on the C++11 random library which may be useful to you. 编辑:我在C ++ 11随机库上找到了一个很好的资源 ,可能对你有用。

There is no reason not to do it like this. 没有理由不这样做。 Depending on which random generator you use, the period is quite huge (2^19937 in case of Mersenne-Twister), so in most cases, you won't even reach the end of one period during the execution of your program. 根据您使用的随机发生器,周期非常大(在Mersenne-Twister的情况下为2 ^ 19937),因此在大多数情况下,您甚至不会在执行程序期间达到一个周期的结束。 And even if it is not said that, it's worse to reach the period with all distributions using the same generator than having 3 generators each doing 1/3 of their period. 即使没有这样说,使用相同的发生器到达所有分布的时间比使用3个发生器每个进行1/3的周期更糟糕。

In my programs, I use one generator for each thread, and it works fine. 在我的程序中,我为每个线程使用一个生成器,它工作正常。 I think that's the main reason they split up the generator and distributions in C++11, since if you weren't allowed to do this, there would be no benefit from having the generator and the distribution separate, if one needs one generator for each distribution anyway. 我认为这是他们在C ++ 11中拆分生成器和发行版的主要原因,因为如果你不允许这样做,那么将发生器和发行版分开是没有好处的,如果需要一个发生器无论如何每个分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM