InverseLight / ValoMC

Monte Carlo software for simulating light propagation
https://inverselight.github.io/ValoMC/
Other
43 stars 14 forks source link

Correlation in RNG #12

Open hanninenn opened 3 years ago

hanninenn commented 3 years ago

If seed for RNG is not given manually, simulations started at the same second will give identical result. Further, there seems to be correlation between simulations which are started within a couple of seconds.

tlunttil commented 3 years ago

PRNG initialisation uses time(), which has granularity of a second. A better way would be to use random_device (available since C++11).

Correlation between runs started a couple of seconds apart seems to be a bug: on line 1518 in MC2D.cpp and 1641 in MC3D.hpp variable totalthreadsis always 1 in non-MPI runs. Because of this, if two OpenMP runs are started a few seconds apart, some threads in the second run will use the same PRNG seeds as some threads in the first run. This could be fixed by setting totalthreads correctly (to nthreads) in non-MPI runs. Alternatively, if the PRNG initialisation is done with some better method than time(), this problem is also fixed automatically.