C++ rand on microtime?

Started by
15 comments, last by random_thinker 18 years, 8 months ago
One problem with that approach is that if that random number comes from rand(), all you have achieved is to seed the processes with slightly offset sequences.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
If you want to reseed the PRNG in each process, you can use the pid as part of the seed (xor'ed with the time, for example). This will ensure that different processes get different seeds.
Quote:Original post by Fruny
One problem with that approach is that if that random number comes from rand(), all you have achieved is to seed the processes with slightly offset sequences.

Which is why I said "or a modified version of [what is returned from rand()]", such as inverting the bits, rotating them over 5 bits, and xoring them with a randomish constant. Or something weird. Wouldn't that effectively negate the offset-sequence phenomenon sufficiently for the non-rigorous use that is needed here?
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
"Randomish Constant" That is pretty funny. I know what you mean though, but those two words together are pretty funny.
As I understand it rand() is a pretty crappy random number generater anyway. So for something better try here and seed with anything that will be different in trial.
As a starting point for a fast random number generator, do a search for 'taus88' or 'tausme2'. Taus 88 (with period 2^88) is a very fast tausworth generator developed by L'ecuyer (Montreal) and is one of my favourites for general purpose variates. Get the paper 'tausme2.ps' as it contains the code and 'correct' initialization info. It is very fast; probably 40 to 50% faster than rand when optimized.

The mrg319 is another one from L'ecuyer but is slower than rand(); it appears to be the basis for L'ecuyer's own software. However, both of these have been through extensive testing for randomness. Also, Marsaglia and Tseng have produced some superb variate generators. An excellent reference with background can be found in 'Marsaglia, George, 'KISS random number generator', sci.math : The Math Forum, Jan 17, 2003.' This provides a very fast, extensively tested mwc with period 2^33242, as well as others. All of this stuff can be downloaded from the 'net. The Journal of Statistical Software is on the net and contains excellent information on derived variates.

For a serious random number system, you need to be able to save the seed state and reload it for a later run. It is sometimes necessary to repeat the stream exactly to check your results. Additionally, there is no such thing as a 'perfect' generator. Cryptology requires total randomness, so that the series of numbers can not be repeated regardless of the seed, whereas Monte Carlo requires 'repeatable randomness' if you know what I mean. I follow Knuth's advice; roughly that the best way to test a random number system is to repeat the run with different generators. Another source of good technology in this area is 'Numerical Recipes in C++", parts of which can be accessed on the net via Cambridge University. You may also want to look at the Mersenne Twister (Japan). It is a tgfsr with period 2^19000+ (I think) that it very popular in commercial apps, and is also very fast.

One of the more difficult tasks is to be able to repeat the number stream for all variates, base and derived, from a single seed. This includes normal, exponential, gamma, etc. You may find this a struggle, as some of the best algorithms do not 'process' base random numbers, but instead use other methods.

Be careful in the selection of generators from the net because there's a lot of rubbish out there that people still use. Be warned!

I really don't think that it matters what you use to seed your generator. I personally use rand() because it is easy to to set srand to time(0) as you have done. You can then save this value as the state and repeat the trial from there. I prefer to concentrate more on the best base integer generators instead for producing the stream.

Using /dev/random can be an option, but as was stated earlier this depends upon the entropy of the system and can have interrupts. I've never personally used it mainly because I do cross platform work and /dev/random is a Unix/Mac thing. I don't like to be tied down to platform-specific issues as I use three (WinXP/Linux/MacOSX) in my work, so my code has to be portable. However I do all my development on Gentoo.

Best of Luck!!

--random

--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
As a thought, if you need comnpletely distinct random processes, why not use 2 different generators, seed one and use it to seed instances of the other?

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.

This topic is closed to new replies.

Advertisement