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);
} 5 12173
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);
}
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); }
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jamie Fryatt |
last post by:
Hi everyone, here's what id like to do.
I have a table with 2 fields, name and value
I need to be able to add multiple records quickly, for example I need to add
name value
abc 1...
|
by: sp0 |
last post by:
Is there a reason why to make mix numbers improper when adding?
It seems when subtracting and adding, adding a subtracting the whole
numbers and fraction parts should be sufficient? what'ch think
|
by: Tim Quon |
last post by:
Hi
I have a function which needs a struct timeVal as timeout parameter.
How can I initialize this struct to a timeout of 3 seconds?
Thanks
Tim
|
by: Jim Heavey |
last post by:
Trying to figure out the technique which should be used
to add rows to a datagrid. I am thinking that I would
want an "Add" button on the footer, but I am not quite
sure how to do that. Is that...
|
by: Tony Houghton |
last post by:
I'm writing a python program which reads input device events so it needs
to know sizeof(struct timeval). By using the struct module I should be
able to work out sizeof(long) from python, but I...
|
by: Kadett |
last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control
at run-time and filling it with some records (let's say 10 000). This
operation seems to be quite fast, but when I call...
|
by: AndyL69 |
last post by:
Hello
I've got a very strange Problem. When im adding a new ACE entry to a UNC Direcotry the inherented ACL's will be lost. When I'm adding a new ACE to a directory / file in this UNC path the...
|
by: sapsi |
last post by:
Hi,
I am using a library (pcapy) that returns a timeval object T=
(seconds,microseconds) where microseconds is always < 1e6.
Is there a Python class that can handle timeval structs? Specifically,...
|
by: PawelCarqowski |
last post by:
Hallo group members,
Do You know any conversion specification for this. I imagine:
struct timeval time;
printf("%T\n", time);
regards,
Pawel
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |