[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

random milliseconds between 0 and 4 seconds.

Joe, G.I.

10/10/2008 3:30:00 PM

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.
24 Answers

Joe, G.I.

10/10/2008 4:30:00 PM

0

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 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

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

Valeri Sokolov

10/10/2008 8:42:00 PM

0

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 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

Ian Collins

10/10/2008 8:42:00 PM

0

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 help out on how to do this? Any help much appreciated.

What have you tried so far?

--
Ian Collins

Valeri Sokolov

10/10/2008 8:52:00 PM

0

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 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.

Kai-Uwe Bux

10/10/2008 10:59:00 PM

0

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 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

Juha Nieminen

10/11/2008 8:49:00 AM

0

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?

Joe, G.I.

10/11/2008 5:59:00 PM

0

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 help out on how to do this? Any help 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;
}

Joe, G.I.

10/11/2008 6:11:00 PM

0


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;
}

Joe, G.I.

10/11/2008 6:34:00 PM

0


>> 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.

Joe, G.I.

10/11/2008 6:38:00 PM

0

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

Thanks again