From 83841f4934e44a5b78beb99156d4a9b6b3619024 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 30 Jan 2014 23:35:54 -0800 Subject: [PATCH] cmMath.h.c : Added cmRandXXX() functions. --- cmMath.c | 28 ++++++++++++++++++++++++++++ cmMath.h | 7 +++++-- 2 files changed, 33 insertions(+), 2 deletions(-) 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