diff --git a/cmMath.c b/cmMath.c index afec115..2ac7a82 100644 --- a/cmMath.c +++ b/cmMath.c @@ -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))); +} diff --git a/cmMath.h b/cmMath.h index 0fb0f79..4d3697f 100644 --- a/cmMath.h +++ b/cmMath.h @@ -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