# C++ rand on microtime?

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

## 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 on other sites
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 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 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 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 on other sites
/dev/random is a device that produces an unlimited sequence of random values. You can read it like it was a file.

##### 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 on other sites
Quote:
 Original post by Inmate2993Don'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 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 on other sites
Quote:
 Original post by l0kiThis 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 11
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015839
×