From 4feff2411d1b703a3b93d8a342112bd998b1ffed Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 20 Jan 2013 21:33:48 +0000 Subject: Made the math::randomf() method inline, added a ranged variant. --- src/math/functions.cc | 11 ----------- src/math/functions.h | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 22 deletions(-) (limited to 'src/math') diff --git a/src/math/functions.cc b/src/math/functions.cc index 7c95a79..ffbf453 100644 --- a/src/math/functions.cc +++ b/src/math/functions.cc @@ -5,23 +5,12 @@ */ #include "math/functions.h" -#include namespace math { const float DELTA = 10e-10; -float randomf(const float max) -{ - return ((float) rand() / (float) RAND_MAX) * max; -} - -unsigned randomi(const unsigned int max) -{ - return ((unsigned int)(rand() % max)); -} - float degrees180f(float angle) { float r = angle; diff --git a/src/math/functions.h b/src/math/functions.h index 8135392..44ec1fa 100644 --- a/src/math/functions.h +++ b/src/math/functions.h @@ -7,8 +7,7 @@ #ifndef __INCLUDED_MATH_FUNCTIONS_H__ #define __INCLUDED_MATH_FUNCTIONS_H__ -#include - +#include #include #include @@ -63,17 +62,35 @@ inline const long max(const long a, const long b) return (a > b ? a : b); } -/// returns a random float -/** The value returned will be in the interval [0-max] - * @param max the maximum value returned - **/ -float randomf(const float max = 1.0f); + /** + * @brief returns a random float + * The value returned will be in the interval [0-max] + * @param max the maximum value returned + * */ +inline float randomf(const float max = 1.0f) +{ + return ((float) rand() / (float) RAND_MAX) * max; +} + + /** + * @brief returns a random float + * The value returned will be in the interval [min-max] + * */ +inline float randomf(const float min, const float max) +{ + return (min + randomf(max - min)); +} -/// returns a random integer -/** The value returned will be in the interval [0-(max-1)] +/** + * @brief returns a random integer + * The value returned will be in the interval [0-(max-1)] * @param max the maximum value returned - **/ -unsigned int randomi(const unsigned int max); + * */ + +inline unsigned randomi(const unsigned int max) +{ + return ((unsigned int)(rand() % max)); +} /// return the sign of a float value float sgnf(float value); -- cgit v1.2.3