cmMath.h.c : Added cmRandXXX() functions.

This commit is contained in:
kevin 2014-01-30 23:35:54 -08:00
parent e65764d7a4
commit 83841f4934
2 changed files with 33 additions and 2 deletions

View File

@ -391,3 +391,31 @@ double cmFfSwapULLongToDouble( unsigned long long v )
u.u = cmSwap64(v);
return u.f;
}
int cmRandInt( int min, int max )
{
assert( min <= max );
int offs = max - min;
return min + cmMax(0,cmMin(offs,(int)round(offs * (double)rand() / RAND_MAX)));
}
unsigned cmRandUInt( unsigned min, unsigned max )
{
assert( min <= max );
unsigned offs = max - min;
return min + cmMax(0,cmMin(offs,(unsigned)round(offs * (double)rand() / RAND_MAX)));
}
float cmRandFloat( float min, float max )
{
assert( min <= max );
float offs = max - min;
return min + cmMax(0,cmMin(offs,(float)(offs * (double)rand() / RAND_MAX)));
}
double cmRandDouble( double min, double max )
{
assert( min <= max );
double offs = max - min;
return min + cmMax(0,cmMin(offs,(offs * (double)rand() / RAND_MAX)));
}

View File

@ -62,6 +62,9 @@ float cmFfSwapUIntToFloat( unsigned v );
unsigned long long cmFfSwapDoubleToULLong( double v );
double cmFfSwapULLongToDouble( unsigned long long v );
//=================================================================
int cmRandInt( int min, int max );
unsigned cmRandUInt( unsigned min, unsigned max );
float cmRandFloat( float min, float max );
double cmRandDouble( double min, double max );
#endif