random milliseconds between 0 and 4 seconds.

K

Kai-Uwe Bux

Joe said:
For some reason, this still doesn't work. I think the problem is that
time() measures in seconds, yet my start_time + exec_time is adding
hundreds or thousands of milliseconds to seconds, so my now time will
never surpass end_time for a long time.

How should I go about fixing that. I would like the millisecond accuracy
between times.
[code snipped]

I don't know about guaranteed accuracy. But you could use double and seconds
throughout:

#include <ctime>
#include <iostream>
#include <ostream>
#include <cstdlib>

double get_random ( double bound ) {
// WARNING: not uniform
return ( bound * ( std::rand() / (RAND_MAX+1.0) ) );
}

double const max_seconds = 4.0;

double seconds ( clock_t ticks ) {
return ( double( ticks ) / double( CLOCKS_PER_SEC ) );
}

double get_seconds ( void ) {
return ( seconds( clock() ) );
}

int main ( void ) {
std::srand( time(0) );
double start = get_seconds();
double random_time = get_random( max_seconds );
std::cout << start << '\n';
std::cout << random_time << '\n';
while ( get_seconds() - start < random_time ) {
}
std::cout << get_seconds() << '\n';
}


Best

Kai-Uwe Bux
 
J

James Kanze

Then you have to go for a platform specific solution.

Or use a portable library which supports it. (I think Boost has
something along these lines, but I'm not sure.)
 
J

James Kanze

I've found it easier to generate integer random values by
using the modulo value:
#define MAX_MILLISECONDS 4000
// gives random_number value of 0-3999
int random_number = rand() % MAX_MILLISECONDS;
// or, if you want a value 1-4000, do this:
// returns random_number value 1-4000
int random_number = 1+ rand() % MAX_MILLISECONDS;

Note that this introduces a bias. The bias is very, very small
when the modulo is very small compared to RAND_MAX, but I'm not
at all sure that you can ignore it when the modulo is 4000.

More generally, unless explicit steps are taken to avoid it, any
means of converting an linear distribution of numbers in the
range 0...RAND_MAX to the range 0...N will introduce a bias
unless N is an exact divisor of RAND_MAX. Generally, you need
something like:

int const limit = RAND_MAX - RAND_MAX % N ;
int result = rand() ;
while ( result >= limit ) {
result = rand() ;
}
return result % RAND_MAX ;
 
K

Kai-Uwe Bux

James said:
Note that this introduces a bias. The bias is very, very small
when the modulo is very small compared to RAND_MAX, but I'm not
at all sure that you can ignore it when the modulo is 4000.

More generally, unless explicit steps are taken to avoid it, any
means of converting an linear distribution of numbers in the
range 0...RAND_MAX to the range 0...N will introduce a bias
unless N is an exact divisor of RAND_MAX. Generally, you need
something like:

int const limit = RAND_MAX - RAND_MAX % N ;
int result = rand() ;
while ( result >= limit ) {
result = rand() ;
}
return result % RAND_MAX ;

Just a nit:

return result % N

Also a nit: there seems to be a mixup. The target range should be [0,N) not
[0,N].


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top