diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index 5ecbbfe..8c2355c 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -2197,11 +2197,7 @@ luaFunc(randRange) { int n1 = lua_tointeger(L, 1); int n2 = lua_tointeger(L, 2); - int spread = n2-n1; - - int r = rand()%spread; - r += n1; - + int r = randRange(n1, n2); luaReturnNum(r); } diff --git a/BBGE/Base.cpp b/BBGE/Base.cpp index e428c7b..913ad2a 100644 --- a/BBGE/Base.cpp +++ b/BBGE/Base.cpp @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Core.h" #include #include "OSFunctions.h" +#include "Randomness.h" #if SDL_VERSION_ATLEAST(2,0,0) #include "SDL_filesystem.h" @@ -41,14 +42,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int randAngle360() { - return rand()%360; + unsigned r = Randomness::getBits32(); + return r%360; } int randRange(int n1, int n2) { - int r = rand()%(n2-1); - r += n1; - return r; + unsigned lo = std::min(n1, n2); + unsigned hi = std::max(n1, n2); + unsigned r = Randomness::getBits32(); + r %= (hi - lo + 1u); + return r + lo; } std::string removeSpaces(const std::string &input) @@ -311,7 +315,9 @@ bool chance(int perc) { if (perc == 100) return true; if (perc == 0) return false; - return ((rand()%100) <= perc); + + unsigned r = Randomness::getBits32(); + return ((r%100) <= perc); } void errorLog(const std::string &s) @@ -422,7 +428,8 @@ bool isTouchingLine(Vector lineStart, Vector lineEnd, Vector point, int radius, Vector randVector(float mag) { - float angle = (rand() / (float)RAND_MAX) * 2.0f * PI; + float r = Randomness::getFloat01(); + float angle = 2.0f * PI * r; float x = sinf(angle), y = cosf(angle); return Vector(x*mag, y*mag); } diff --git a/BBGE/Base.h b/BBGE/Base.h index fd681f7..ac0684d 100644 --- a/BBGE/Base.h +++ b/BBGE/Base.h @@ -179,7 +179,7 @@ int randAngle360(); Vector randVector(float magnitude); std::string splitCamelCase(const std::string &input); std::string removeSpaces(const std::string &input); -int randRange(int r1, int r2); +int randRange(int r1, int r2); // from..to inclusive enum LerpType {