-
Random Numbers
I just got done reading this about how to correctly use rand() and time(). I notice that I compiler warnings about converting to int from double.
So to resolve the warnings I've explicitly cast my return statement in these functions:
Code:
int get_rand( int UPPER_BOUND ){
return (int)( uniform_deviate( rand() ) * UPPER_BOUND );
}
int get_rand( int LOWER_BOUND, int UPPER_BOUND ){
return (int)( LOWER_BOUND + uniform_deviate( rand() ) * ( UPPER_BOUND - LOWER_BOUND ) );
}
Is this the correct way to cast the returns or should i use <static_cast>? Or is there another more correct solution?
-
-
For C++, it's better to use static_cast, yes... It's preferable to avoid casts and foremost, C-style casts.
-
Great link! I'll be busy reading through for a while. Mucho gracias!