Jump to content
  • Advertisement
Sign in to follow this  
l0ki

C++ rand on microtime?

This topic is 4850 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

Is there a way to srand() based on microtime? I have:
if(varMax > 0){
    srand(time(NULL));
    // calc a random variable set number between 0 and varMax.
    this->page[iCount]->m_varSet = ((rand() % varMax)+1);
}else{
}
However, this is in a program that can have new processes spawned with only a milliseconds difference in time, the problem is that m_varSet is always choosing the same number between multiple processes because of this time issue, so how does srand(time(NULL)) seed, seconds? TIA.

Share this post


Link to post
Share on other sites
Advertisement
If you are using windows, you can use the high performance counter. First you have to see if you hardware supports it using this function then you can query it using this funtion.

hope this helps

~guyaton

Share this post


Link to post
Share on other sites
Aye under Windows I know how to do it, unfortunately this is being dev'd on RHE4 linux, and the simple routes don't apply :D

Share this post


Link to post
Share on other sites
What's your program doing? You probably won't get very good random numbers that way if you're concerned with each instance having different sequences.
If you're only using linux/unix why not read from /dev/random to seed srand()?

Share this post


Link to post
Share on other sites
This process is a testing agent studying web site performance under stress, each of these is basically a user journey through a website. What I am choosing is a set of parameters to post to the server, but I want this to be different each time. However, because these processes are started within milliseconds of each other, the second count doesn't tick over and I'm seeing results based on the same sets chosen by rand() :(

I'm not sure what /dev/random is, I'll have to look into that, I'm new to linux programming. ta.

Share this post


Link to post
Share on other sites
Don't re-seed Rand. Some randomizers have the significant flaw that when reseeded with sequential seeds, they report very similar sequences that take a lot of time to diverge. If this is beyond your control, store the last value rand reported and reseed with that, so you can continue the sequence.

Share this post


Link to post
Share on other sites
Quote:
Original post by Inmate2993
Don't re-seed Rand. ...

I though there might be something like that. Using /dev/random to seed srand seems like an elegant solution on linux/unix.

Share this post


Link to post
Share on other sites
Alternatively, and since /dev/random blocks when it runs out of entropy, you can read all your random numbers from /dev/urandom.

You could even use the boost::random module of the Boost library - it has a bunch of different RNG algorithms, adaptors and can be seeded directly from a device file.

Share this post


Link to post
Share on other sites
Quote:
Original post by l0ki
This process is a testing agent studying web site performance under stress, each of these is basically a user journey through a website. What I am choosing is a set of parameters to post to the server, but I want this to be different each time. However, because these processes are started within milliseconds of each other, the second count doesn't tick over and I'm seeing results based on the same sets chosen by rand() :(

So you have a process that creates new processes (fork, whatever, I'm a Windows guy), and each of these processes then seeds its own random sequence with the time? You could instead have the original process seed its random sequence, then pass a random number to each process that it creates that actually runs the journey through the website. Each process can take this number and use it (or a modified version of it) as the seed for their random sequence, rather than using the time. This would ensure that each one got a significantly different random number to use as a seed.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!