random milliseconds between 0 and 4 seconds.

J

Joe, G.I.

Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
 
J

Joe, G.I.

Valeri said:
You're trying to generate about 50 random numbers between 0 and 4000,
don't you? o_O

No, 0 seconds to 4 seconds. 50 separate random times between 0.0 and 4.0
seconds.
 
V

Valeri Sokolov

Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

You're trying to generate about 50 random numbers between 0 and 4000,
don't you? o_O
 
I

Ian Collins

Joe said:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

What have you tried so far?
 
V

Valeri Sokolov

Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

Another interpretation: to generate 50 random numbers n_i so, that
their sum equals 4000.
 
K

Kai-Uwe Bux

Joe said:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

a) What is the desired type:

- double
- long double
- float
- (unsigned int) with 4 being 4000ms


b) Once you settled on the type, write a function that generates one random
number in the desired range. Some care is needed to get this uniform
(which, I think, is an implicit requirement of yours).

c) Call that function 50 times.

d) If you do not need 50 durations, but 50 points on the time arrow, you
might want to sort the 50 results so that they line up nicely. Note that
you might have doubles.

e) If you need 50 distinct values between 0 and 4, you could use a std::set
and insert every random value as it is generated. Iterate until the size is
50.


Best

Kai-Uwe Bux
 
J

Juha Nieminen

Joe said:
No, 0 seconds to 4 seconds. 50 separate random times between 0.0 and 4.0
seconds.

Your question is extremely confusing. Are you trying to simply
generate random values inside a given range? If so, then what does that
have to do with seconds or milliseconds? (The milliseconds may be
related to what you will use the random value *for*, but it has nothing
to do with *how* to create random values inside a range value.)

Your value range is also confusing. Getting values in the range 0-4000
(and them dividing by 1000.0) sounds like what you want, but you say
that you don't want that. Then what is it that you want?
 
J

Joe, G.I.

Ian said:
What have you tried so far?

Here's what I'm doing. I'm trying to generate in this example 100 random
values between 0 and 4 seconds. My problem is that my times (generated
random numbers) are all coming out to the same value.



srand(time(0));

for (int i = 0; i < 100; i++) {
ClassA *a = new ClassA();
a->set_start();
a->set_execute();
}


bool ClassA::set_execute()
{
exec_time = generate_random();

return true;
}

int ClassA::generate_random()
{
int r = (rand() / (RAND_MAX + 1.0) * 4000);

printf("r = %d\n", r);

return r;
}
 
J

Joe, G.I.

I also tried this (in combination w/ my other post), this is just the
generating function I keep calling. But I still keep getting the same
number, here it's 1.

int ClassA::generate_random()
{
int random_integer;
int lowest = 1, highest = 10;

int range = highest - lowest + 1;

random_integer = lowest + int (range * rand() / (RAND_MAX + 1.0));

printf("r = %d\n", random_integer);

return random_integer;
}
 
J

Joe, G.I.

a) You leak memory big time in this code. At each iteration, a new ClassA
object is created. Pointers to these objects are not kept.

b) The method set_start() is not given. I shall assume that it does noting.


a) Why is there a return value?

b) Why is this initialization not part of the constructor of ClassA?



a) That will not generate a uniform distribution (unless RAND_MAX has an
unlikely value). However, it might be close enough for you purposes.

b) Why is this not static?


The following prints different values:

#include <stdio.h>
#include <stdlib.h>

int main ( void ) {
srand( 0 );
for ( int i = 0; i < 100; ++i ) {
int r = ( rand() / (RAND_MAX + 1.0) * 4000 );
printf("r = %d\n", r );
}
}

Since your code sample does not compile, it is hard to spot where it
departs.

It compiles for me, but I'm doing something terribly wrong, because even
your code generates the same numbers for me. I know it's my fault, so
let me look elsewhere in here for my problems.

Thanks for you reply.
 
J

Joe, G.I.

Ok, found it, I was using srand(time(0)) in my constructor. But I think
now that that only gets called once.

Thanks again
 
J

Joe, G.I.

Anyone see anything wrong w/ the way I'm checking the differences in
elapsed time? I'm trying to see if the exec_time has elapsed since the
start_time.

Is exec_time correct? I'm trying to get a random number of seconds
between 0 and 4 seconds, random milliseconds, I can't use rounded off
1.0, 2.0, 3.0, 4,0 seconds, but 1.5, 2.3, 3.3, etc...

Using this, my exec_time is always a small number like 850, 3200,
2311, etc... and the start_time is a number like 1223755469, and now
is similiar. So my exec_time will never be greater than the elapsed_time.

Anyone help out?

---
// a random number of seconds between 0 and 4
int const MAX_SECONDS = 4000;

time_t now;
time_t start_time;
time_t exec_time;

start_time = time(0);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_SECONDS);

time(&now);

elapsed_time = difftime(start_time, now);

if (exec_time > elapsed_time) {
// if the time now is longer than the time between start + seconds
// from 0 to 4 ...
}
 
S

Spacefish

maybe he tries to generate 50 numbers in a timerange of 0-4 seconds..
but 0 sec´s is impossible because you can´t do something in no time.
(ok you can if you have the proper boss ;-))
 
J

Joe, G.I.

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.

---

int const MAX_MILLISECONDS = 4000;

start_time = time(NULL);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);

time_t now;
double elapsed_time;

time_t end_time = start_time + exec_time;

time(&now);

// not used now, but reports elapsed seconds
// elapsed_time = difftime(now, _start_time);

if (now > end_time) {
return true;
}

return false;
 
J

Joe, G.I.

Sort of a typo, should read ...

...
int const MAX_MILLISECONDS = 4000;

start_time = time(NULL);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
...


bool check_time()
{
time_t now;
double elapsed_time;

time_t end_time = start_time + exec_time;

time(&now);

// not used now, but reports elapsed seconds
// elapsed_time = difftime(now, _start_time);

if (now > end_time) {
return true
}

return false;
}
 
K

Kai-Uwe Bux

Joe said:
Here's what I'm doing. I'm trying to generate in this example 100 random
values between 0 and 4 seconds. My problem is that my times (generated
random numbers) are all coming out to the same value.



srand(time(0));

for (int i = 0; i < 100; i++) {
ClassA *a = new ClassA();
a->set_start();
a->set_execute();
}

a) You leak memory big time in this code. At each iteration, a new ClassA
object is created. Pointers to these objects are not kept.

b) The method set_start() is not given. I shall assume that it does noting.
bool ClassA::set_execute()
{
exec_time = generate_random();

return true;
}

a) Why is there a return value?

b) Why is this initialization not part of the constructor of ClassA?

int ClassA::generate_random()
{
int r = (rand() / (RAND_MAX + 1.0) * 4000);

a) That will not generate a uniform distribution (unless RAND_MAX has an
unlikely value). However, it might be close enough for you purposes.

b) Why is this not static?
printf("r = %d\n", r);

return r;
}

The following prints different values:

#include <stdio.h>
#include <stdlib.h>

int main ( void ) {
srand( 0 );
for ( int i = 0; i < 100; ++i ) {
int r = ( rand() / (RAND_MAX + 1.0) * 4000 );
printf("r = %d\n", r );
}
}

Since your code sample does not compile, it is hard to spot where it
departs.



Best

Kai-Uwe Bux
 
I

Ian Collins

Joe said:
Anyone see anything wrong w/ the way I'm checking the differences in
elapsed time? I'm trying to see if the exec_time has elapsed since the
start_time.

Is exec_time correct? I'm trying to get a random number of seconds
between 0 and 4 seconds, random milliseconds, I can't use rounded off
1.0, 2.0, 3.0, 4,0 seconds, but 1.5, 2.3, 3.3, etc...

Using this, my exec_time is always a small number like 850, 3200,
2311, etc... and the start_time is a number like 1223755469, and now
is similiar. So my exec_time will never be greater than the elapsed_time.

Anyone help out?

---
// a random number of seconds between 0 and 4
int const MAX_SECONDS = 4000;

time_t now;
time_t start_time;
time_t exec_time;

start_time = time(0);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_SECONDS);
Use an end time:

time_t end_time = start_time + exec_time;
 
K

Kai-Uwe Bux

Joe said:
Anyone see anything wrong w/ the way I'm checking the differences in
elapsed time? I'm trying to see if the exec_time has elapsed since the
start_time.

Is exec_time correct? I'm trying to get a random number of seconds
between 0 and 4 seconds, random milliseconds, I can't use rounded off
1.0, 2.0, 3.0, 4,0 seconds, but 1.5, 2.3, 3.3, etc...

Using this, my exec_time is always a small number like 850, 3200,
2311, etc... and the start_time is a number like 1223755469, and now
is similiar. So my exec_time will never be greater than the elapsed_time.

Anyone help out?

Shouldn't the name be MAX_MILLISECONDS?
time_t now;
time_t start_time;
time_t exec_time;

start_time = time(0);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_SECONDS);

time(&now);

elapsed_time = difftime(start_time, now);

According to the man page, difftime() gets you the time difference in
seconds as a double.
if (exec_time > elapsed_time) {

exec_time is a random integer and measures time in milliseconds. There is an
obvious mismatch to elapsed_time. I doubt that the comparison is
meaningfull.
// if the time now is longer than the time between start + seconds
// from 0 to 4 ...
}


Best

Kai-Uwe Bux
 
I

Ian Collins

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.
Then you have to go for a platform specific solution.
 
D

Default User

Joe said:
Ok, found it, I was using srand(time(0)) in my constructor. But I
think now that that only gets called once.

That's why you should post a complete, minimal program that
demonstrates the problem.




Brian
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top