1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-02-15 09:05:52 +00:00

Base functions use the new randomness now.

- randRange() is no longer broken.
- Lua randRange() uses the same function now and is no longer off-by-1
  (exclusive; right end never reached)
This commit is contained in:
fgenesis 2025-02-01 06:09:37 +01:00
parent 9664352a11
commit 8a5f8d2ac7
3 changed files with 15 additions and 12 deletions

View file

@ -2197,11 +2197,7 @@ luaFunc(randRange)
{ {
int n1 = lua_tointeger(L, 1); int n1 = lua_tointeger(L, 1);
int n2 = lua_tointeger(L, 2); int n2 = lua_tointeger(L, 2);
int spread = n2-n1; int r = randRange(n1, n2);
int r = rand()%spread;
r += n1;
luaReturnNum(r); luaReturnNum(r);
} }

View file

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "Core.h" #include "Core.h"
#include <assert.h> #include <assert.h>
#include "OSFunctions.h" #include "OSFunctions.h"
#include "Randomness.h"
#if SDL_VERSION_ATLEAST(2,0,0) #if SDL_VERSION_ATLEAST(2,0,0)
#include "SDL_filesystem.h" #include "SDL_filesystem.h"
@ -41,14 +42,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
int randAngle360() int randAngle360()
{ {
return rand()%360; unsigned r = Randomness::getBits32();
return r%360;
} }
int randRange(int n1, int n2) int randRange(int n1, int n2)
{ {
int r = rand()%(n2-1); unsigned lo = std::min(n1, n2);
r += n1; unsigned hi = std::max(n1, n2);
return r; unsigned r = Randomness::getBits32();
r %= (hi - lo + 1u);
return r + lo;
} }
std::string removeSpaces(const std::string &input) std::string removeSpaces(const std::string &input)
@ -311,7 +315,9 @@ bool chance(int perc)
{ {
if (perc == 100) return true; if (perc == 100) return true;
if (perc == 0) return false; if (perc == 0) return false;
return ((rand()%100) <= perc);
unsigned r = Randomness::getBits32();
return ((r%100) <= perc);
} }
void errorLog(const std::string &s) void errorLog(const std::string &s)
@ -422,7 +428,8 @@ bool isTouchingLine(Vector lineStart, Vector lineEnd, Vector point, int radius,
Vector randVector(float mag) 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); float x = sinf(angle), y = cosf(angle);
return Vector(x*mag, y*mag); return Vector(x*mag, y*mag);
} }

View file

@ -179,7 +179,7 @@ int randAngle360();
Vector randVector(float magnitude); Vector randVector(float magnitude);
std::string splitCamelCase(const std::string &input); std::string splitCamelCase(const std::string &input);
std::string removeSpaces(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 enum LerpType
{ {