I'm trying to send signals between processes and I need to allow a random time delay of between 0.01 and 0.1 seconds in my signal generation loop. So this is what I'm doing and it is most certainly not between 0.01 and 0.1, but comes out as 1 second. Not sure what I'm missing here.
sleepTime = 100000L+(long)((1e6-1e5)*rand()/(RAND_MAX+1.0));
usleep(sleepTime);
If you've got C++11:
#include <thread>
#include <random>
#include <chrono>
int main()
{
std::mt19937_64 eng{std::random_device{}()}; // or seed however you want
std::uniform_int_distribution<> dist{10, 100};
std::this_thread::sleep_for(std::chrono::milliseconds{dist(eng)});
}
It may not be what your prof is looking for. :-)
All of your constants are 10x too large! Try
sleepTime = 10000L+(long)((1e5-1e4)*rand()/(RAND_MAX+1.0));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With