By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,408 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,408 IT Pros & Developers. It's quick & easy.

adding timeval's

P: n/a
Joe
I'm trying to add the current time + a randomly generated time w/
millisecond precision, but the numbers aren't making sense.

If I add rand_time + now_time on my calculator, I get a different result
than what's in sched_time. Anyone see what I'm doing wrong?

These are my results ..

rand_time = 4958.128418
now_time = 1225033344.000000
sched_time = 1225038336.000000
(my calc says rand_time + now_time = 1225038302.13)

struct timeval now;

gettimeofday(&now, NULL);

float rand_time = random_time();
float now_time = now.tv_sec + (now.tv_usec / 1000000.0);
float sched_time = now_time + rand_time;

printf("rand_time = %f\n", rand_time);
printf("now_time = %f\n", now_time);
printf("sched_time = %f\n", sched_time);

float random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}
Oct 26 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Joe wrote:
I'm trying to add the current time + a randomly generated time w/
millisecond precision, but the numbers aren't making sense.

If I add rand_time + now_time on my calculator, I get a different result
than what's in sched_time. Anyone see what I'm doing wrong?
Yeah, you're using floats, which on most platforms don't have enough
precision to deal with your data. Try using doubles instead.
>
These are my results ..

rand_time = 4958.128418
now_time = 1225033344.000000
sched_time = 1225038336.000000
(my calc says rand_time + now_time = 1225038302.13)

struct timeval now;

gettimeofday(&now, NULL);

float rand_time = random_time();
float now_time = now.tv_sec + (now.tv_usec / 1000000.0);
float sched_time = now_time + rand_time;

printf("rand_time = %f\n", rand_time);
printf("now_time = %f\n", now_time);
printf("sched_time = %f\n", sched_time);

float random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}
Oct 26 '08 #2

P: n/a
Joe
Ok, I changed them to doubles, and have another question if I may.

I'm randomly generating a value between 0 and 5 seconds (or 5000
milliseconds). Am I properly adding the number of milliseconds to the
current time. Somehow I think I'm adding them wrongly and the sched_time
(future time) is turning out to be much longer in time that 0-5 seconds
from the current_time (now_time).

struct timeval now;

gettimeofday(&now, NULL);

double rand_time = random_time();
double now_time = now.tv_sec + (now.tv_usec / 1000000.0);
double sched_time = now_time + rand_time;

double random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}

red floyd wrote:
Joe wrote:
>I'm trying to add the current time + a randomly generated time w/
millisecond precision, but the numbers aren't making sense.

If I add rand_time + now_time on my calculator, I get a different result
than what's in sched_time. Anyone see what I'm doing wrong?
Yeah, you're using floats, which on most platforms don't have enough
precision to deal with your data. Try using doubles instead.
>>
These are my results ..

rand_time = 4958.128418
now_time = 1225033344.000000
sched_time = 1225038336.000000
(my calc says rand_time + now_time = 1225038302.13)

struct timeval now;

gettimeofday(&now, NULL);

float rand_time = random_time();
float now_time = now.tv_sec + (now.tv_usec / 1000000.0);
float sched_time = now_time + rand_time;

printf("rand_time = %f\n", rand_time);
printf("now_time = %f\n", now_time);
printf("sched_time = %f\n", sched_time);

float random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}
Oct 26 '08 #3

P: n/a
Joe wrote:
Ok, I changed them to doubles, and have another question if I may.

I'm randomly generating a value between 0 and 5 seconds (or 5000
milliseconds). Am I properly adding the number of milliseconds to the
current time. Somehow I think I'm adding them wrongly and the sched_time
(future time) is turning out to be much longer in time that 0-5 seconds
from the current_time (now_time).

struct timeval now;

gettimeofday(&now, NULL);

double rand_time = random_time();
double now_time = now.tv_sec + (now.tv_usec / 1000000.0);
double sched_time = now_time + rand_time;

double random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}
The function random_time() will return a (non-uniform) value in [0,5000). If
you interpret that as seconds, then you are adding up to 5000 seconds to
your time. This is probably not what you want.

Hint: If you want to use floating point arithmetic, settle on a dimension
(e.g., seconds) and stick with it. Maybe, you should remind yourself using
a typedef:

typedef double time_in_seconds;

Also instead of building the bound into the function, you could do (still
non-uniform):

time_in_seconds random_time ( time_in_seconds upper ) {
return( rand() * upper / ( RAND_MAX + 1.0 ) );
}

and use random_time( 5.0 ) within client code.
[quote snipped since it is immaterial]

a) Please quote only relevant material.

b) Please do not top-post.
Best

Kai-Uwe Bux
Oct 26 '08 #4

P: n/a
red floyd wrote:
Joe wrote:
>I'm trying to add the current time + a randomly generated time w/
millisecond precision, but the numbers aren't making sense.

If I add rand_time + now_time on my calculator, I get a different result
than what's in sched_time. Anyone see what I'm doing wrong?
Yeah, you're using floats, which on most platforms don't have enough
precision to deal with your data. Try using doubles instead.
Or long long if your system has it.

--
Ian Collins
Oct 26 '08 #5

P: n/a
On Oct 26, 10:40 pm, Ian Collins <ian-n...@hotmail.comwrote:
red floyd wrote:
Joe wrote:
I'm trying to add the current time + a randomly generated
time w/ millisecond precision, but the numbers aren't
making sense.
If I add rand_time + now_time on my calculator, I get a
different result than what's in sched_time. Anyone see what
I'm doing wrong?
Yeah, you're using floats, which on most platforms don't
have enough precision to deal with your data. Try using
doubles instead.
Or long long if your system has it.
If his system has gettimeofday, it has long long:-).

Personally, of course, I'd have just stuck with the timeval;
it's not that difficult to do a correct addition in base
1000000 (since you're pretty much guaranteed that adding two
values < 1000000 will not overflow).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 28 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.