diff options
author | Stijn Buys <ingar@osirion.org> | 2013-01-20 21:33:48 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2013-01-20 21:33:48 +0000 |
commit | 4feff2411d1b703a3b93d8a342112bd998b1ffed (patch) | |
tree | 6a2c42219c9cf8b0501e07cbfed625b49f400817 /src | |
parent | ff57bf67a8e6f39c422b343488c3e90318063d3f (diff) |
Made the math::randomf() method inline, added a ranged variant.
Diffstat (limited to 'src')
-rw-r--r-- | src/math/functions.cc | 11 | ||||
-rw-r--r-- | src/math/functions.h | 39 |
2 files changed, 28 insertions, 22 deletions
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 <stdlib.h> 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 <math.h> - +#include <cmath> #include <cstdlib> #include <cmath> @@ -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); |