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.
C++ rand on microtime?
Is there a way to srand() based on microtime? I have:
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
hope this helps
~guyaton
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
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()?
If you're only using linux/unix why not read from /dev/random to seed srand()?
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.
I'm not sure what /dev/random is, I'll have to look into that, I'm new to linux programming. ta.
/dev/random is a device that produces an unlimited sequence of random values. You can read it like it was a file.
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.
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement