Well, the obvious thing has been stated several times by others, use the new C ++ 11 generators. I repeat this for another reason. You use the output for scientific calculations, and rand usually implements a rather poor generator (while in many implementations of the main thread MT19937 is used, which, in addition to poor state recovery, is not so bad, but you have no guarantee for a specific algorithm, and at least one the main compiler still uses a very bad LCG).
Do not perform scientific calculations with a bad generator. It doesn't really matter if you have things like hyperplanes in your random numbers if you make some kind of stupid game that shoots little birds on your mobile phone, but a lot of time is important for scientific modeling. Never use a bad generator. Do not do this.
Important note: std::random_shuffle (a version with two parameters) can actually cause rand , which is an error you should be aware of if you use it, even if you otherwise use the new C ++ 11 generators found in <random> .
About the actual problem causing srand twice (or even more often) is not a problem. You can basically call him as often as you want, all he does is change the seed and therefore the subsequent pseudorandom sequence. I wonder why the XML library wants to call it at all, but they are right in their answer, it is not illegal for them. But it also does not matter.
The only thing that needs to be done is that either you do not want to receive any particular pseudo-random sequence (that is, any sequence will do, you are not interested in reproducing the exact sequence), or you will be the last srand that cancels any previous calls.
Thus, the implementation of your own generator with good statistical properties and a sufficiently long period of 3-5 lines of code is not so difficult, with a little caution. The main advantage (besides speed) is that you precisely control your state and change it.
It is unlikely that you will need periods much longer than 2 128 because explicit forbidden time actually consumes so many numbers. A 3GHz computer that consumes one number each cycle will run for 10 21 years in a period of 2 128 so for people with average life expectancy not so much. Even assuming that the supercomputer with which you run your simulation is a trillion times faster, your great great children will not survive the end of the period.
While periods like the 2 19937 that the current “modern” generators supply are really funny, trying to improve the generator on the wrong end if you ask me (it’s better to make sure that they are statistically stable and quickly recover from the worst case state, etc. d.). But, of course, opinions here may differ.
This site contains several quick generators with implementations. They are xorshift generators in combination with the step of adding or multiplying and a small (from 2 to 64 machine words) lag, which leads to both fast and high-quality generators (there is also a test suite, and the site author wrote a couple of documents on this subject ) I am using a modification of one of them (the 2-word 128-bit version is ported to 64-bit, with modified changes, respectively).