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.
your code generates the same numbers for me. I know it's my fault, so
let me look elsewhere in here for my problems.