diff options
| author | Machiavelli <machiaveltman@gmail.com> | 2012-01-03 03:52:26 -0800 |
|---|---|---|
| committer | Machiavelli <machiaveltman@gmail.com> | 2012-01-03 03:52:26 -0800 |
| commit | e35fd87d3f83d5d336e50695dfce7461a6e75193 (patch) | |
| tree | 6be6d4c0207980c419adf70592d613beb43b9d7f | |
| parent | 93dbe07cd0fa8958904b7d2cdc3797a913104fb5 (diff) | |
| parent | 29c2dfb1dad310b7388685a7020f303b45a6569f (diff) | |
Merge pull request #4582 from Chaplain/cleanup4
Core/Utilities: Implement random generator for float values.
NOTE: Actually just casts the outcome of int randgen to float
| -rwxr-xr-x | src/server/shared/Utilities/Util.cpp | 25 | ||||
| -rwxr-xr-x | src/server/shared/Utilities/Util.h | 3 |
2 files changed, 21 insertions, 7 deletions
diff --git a/src/server/shared/Utilities/Util.cpp b/src/server/shared/Utilities/Util.cpp index ba385230822..6ae43bc6840 100755 --- a/src/server/shared/Utilities/Util.cpp +++ b/src/server/shared/Utilities/Util.cpp @@ -30,17 +30,22 @@ typedef ACE_TSS<SFMTRand> SFMTRandTSS; static SFMTRandTSS sfmtRand; -int32 irand (int32 min, int32 max) +int32 irand(int32 min, int32 max) { return int32(sfmtRand->IRandom(min, max)); } -uint32 urand (uint32 min, uint32 max) +uint32 urand(uint32 min, uint32 max) { return sfmtRand->URandom(min, max); } -int32 rand32 () +float frand(float min, float max) +{ + return float(sfmtRand->Random() * (max - min) + min); +} + +int32 rand32() { return int32(sfmtRand->BRandom()); } @@ -50,27 +55,33 @@ double rand_norm(void) return sfmtRand->Random(); } -double rand_chance (void) +double rand_chance(void) { return sfmtRand->Random() * 100.0; } + #else typedef ACE_TSS<MTRand> MTRandTSS; static MTRandTSS mtRand; int32 irand(int32 min, int32 max) { - return int32(mtRand->randInt (max - min)) + min; + return int32(mtRand->randInt(max - min)) + min; } uint32 urand(uint32 min, uint32 max) { - return mtRand->randInt (max - min) + min; + return mtRand->randInt(max - min) + min; +} + +float frand(float min, float max) +{ + return float(mtRand->randExc(max - min) + min); } int32 rand32() { - return mtRand->randInt (); + return mtRand->randInt(); } double rand_norm(void) diff --git a/src/server/shared/Utilities/Util.h b/src/server/shared/Utilities/Util.h index b67761c4977..684b26eea63 100755 --- a/src/server/shared/Utilities/Util.h +++ b/src/server/shared/Utilities/Util.h @@ -64,6 +64,9 @@ inline uint32 secsToTimeBitFields(time_t secs) /* Return a random number in the range 0 .. RAND32_MAX. */ int32 rand32(); + /* Return a random number in the range min..max */ + float frand(float min, float max); + /* Return a random double from 0.0 to 1.0 (exclusive). Floats support only 7 valid decimal digits. * A double supports up to 15 valid decimal digits and is used internally (RAND32_MAX has 10 digits). * With an FPU, there is usually no difference in performance between float and double. */ |
