using one random engine for multi distributions in

2019-04-07 01:10发布

问题:

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. 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. Is that OK to use the random engine in this way or I should declare different engines for every distribution I use.

回答1:

It's ok.

Reasons to not share the generator:

  • threading (standard RNG implementations are not thread safe)
  • 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.



回答2:

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 (e.g. x and y) you need randomness in both dimensions. If you take the random numbers from one generator (noise()) and assign them successively

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. 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.

For two further reasons to use different generators look at sehe's answer.



回答3:

There is no reason not to do it like this. Depending 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 in your programm run. And even if it is not said that it is worse to reach the period with all distributions using the same generator then having 3 generators each doing 1/3 of their period.

In my programs I use one generator for each thread and it works fine. I think it's the main reason they split up generator and distributions in C++11. Since if you weren't allowed to do this, there would be no benefit from having generator and distribution separate, if one needs one generator for each distribution anyway.



回答4:

MosteM's answer isn't correct. 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. If you want correlation between two random variables, it's better to build them starting from a common random variable using mathematical principal: e.g., if A, B are independent normal(0,1), then A and aA +sqrt(1-a**2)B are normal(0,1) with correlation a.

EDIT: I found a great resource on the C++11 random library which may be useful to you.



标签: c++ c++11 random