|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 out on how to do this? Any much appreciated. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Valeri Sokolov wrote:
> On Oct 10, 7:30 pm, "Joe, G.I." <invalid.email@address> wrote: >> 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 out on how to do this? Any much appreciated. > > 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Oct 10, 7:30 pm, "Joe, G.I." <invalid.email@address> wrote:
> 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 out on how to do this? Any much appreciated. You're trying to generate about 50 random numbers between 0 and 4000, don't you? o_O |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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 out on how to do this? Any much appreciated. What have you tried so far? -- Ian Collins |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Oct 10, 7:30 pm, "Joe, G.I." <invalid.email@address> wrote:
> 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 out on how to do this? Any much appreciated. Another interpretation: to generate 50 random numbers n_i so, that their sum equals 4000. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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 out on how to do this? Any 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
>> 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. 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? |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Ian Collins wrote:
> Joe, G.I. wrote: >> 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 out on how to do this? Any much appreciated. > > 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; } |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
>> 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. > 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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Ok, found it, I was using srand(time(0)) in my constructor. But I think
now that that only gets called once. Thanks again |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 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 ... } |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 ;-)) ------------------------------------- visit my webpage http://www.howhot.de Joe, G.I. schrieb: > 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 out on how to do this? Any much appreciated. |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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; |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> Ian Collins wrote: >> Joe, G.I. wrote: >>> 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 out on how to do this? Any much appreciated. >> >> 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(); > } > 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 |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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 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; -- Ian Collins |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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 out? > > --- > // a random number of seconds between 0 and 4 > int const MAX_SECONDS = 4000; 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 |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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. -- Ian Collins |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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 |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote:
> 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 |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
On Oct 12, 4:39am, Ian Collins <ian-n...@hotmail.com> wrote:
> Joe, G.I. wrote: > > 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. Or use a portable library which supports it. (I think Boost has something along these lines, but I'm not sure.) -- James Kanze (GABI Software) email:james.kanze@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 |
|
|
|
#23 |
|
Messages: n/a
Hébergeur: |
Joe, G.I. wrote: > Ian Collins wrote: >> Joe, G.I. wrote: >>> Hi all, >>> >>> I'm trying to generate about 50 numbers as milliseconds between 0 and 4 >>> seconds and it's driving me crazy. 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; HTH Carla |
|
|
|
#24 |
|
Messages: n/a
Hébergeur: |
On Oct 14, 3:31 am, Carla Fong <carla.xspamx.f...@verizon.net> wrote:
> Joe, G.I. wrote: > > Ian Collins wrote: > >> Joe, G.I. wrote: > >>> I'm trying to generate about 50 numbers as milliseconds > >>> between 0 and 4 seconds and it's driving me crazy. > 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 ; -- James Kanze (GABI Software) email:james.kanze@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 |
|
|
|
#25 |
|
Messages: n/a
Hébergeur: |
James Kanze wrote:
> On Oct 14, 3:31 am, Carla Fong <carla.xspamx.f...@verizon.net> wrote: >> Joe, G.I. wrote: >> > Ian Collins wrote: >> >> Joe, G.I. wrote: >> >>> I'm trying to generate about 50 numbers as milliseconds >> >>> between 0 and 4 seconds and it's driving me crazy. > >> 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 ; 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 |
|
![]() |
| Outils de la discussion | |
|
|