Jump to content
  • Advertisement
Sign in to follow this  
l0ki

C++ rand on microtime?

This topic is 4947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"Randomish Constant" That is pretty funny. I know what you mean though, but those two words together are pretty funny.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!