Changed love.math.randomnormal([mean], stddev) to love.math.randomNormal(stddev, [mean]);

Changed love.math.randomseed(seed) to love.math.setRandomState(state) and love.math.setRandomState(low, high). Added low, high = love.math.getRandomState().

love.math.setRandomState(state) can set the random state to an integer of up to 53 bits, love.math.setRandomState(low, high) can set the random state to a 64 bit integer by using the first argument as the lower 32 bits and the second argument as the higher 32 bits.
This allows for reliable setting and saving of the state of a RNG.

--HG--
branch : RandomGenerator-2
This commit is contained in:
Alex Szpakowski
2013-08-01 18:32:44 -03:00
parent dfba650575
commit 0447d1e7c5
8 changed files with 143 additions and 90 deletions
+20 -21
View File
@@ -32,32 +32,32 @@ namespace love
namespace math
{
int w_randomseed(lua_State *L)
int w_setRandomState(lua_State *L)
{
uint64 seed = luax_checkrandomseed(L, 1);
Math::instance.randomseed(seed);
Math::instance.setRandomState(luax_checkrandomstate(L, 1));
return 0;
}
int w_getRandomState(lua_State *L)
{
uint32 low = 0, high = 0;
Math::instance.getRandomState(low, high);
lua_pushnumber(L, (lua_Number) low);
lua_pushnumber(L, (lua_Number) high);
return 2;
}
int w_random(lua_State *L)
{
return luax_getrandom(L, 1, Math::instance.random());
}
int w_randomnormal(lua_State *L)
int w_randomNormal(lua_State *L)
{
double mean = 0.0, stddev = 1.0;
if (lua_gettop(L) > 1)
{
mean = luaL_checknumber(L, 1);
stddev = luaL_checknumber(L, 2);
}
else
{
stddev = luaL_optnumber(L, 1, 1.);
}
double stddev = luaL_optnumber(L, 1, 1.0);
double mean = luaL_optnumber(L, 2, 0.0);
double r = Math::instance.randomNormal(stddev);
double r = Math::instance.randomnormal(stddev);
lua_pushnumber(L, r + mean);
return 1;
}
@@ -67,10 +67,7 @@ int w_newRandomGenerator(lua_State *L)
RandomGenerator *t = Math::instance.newRandomGenerator();
if (lua_gettop(L) > 0)
{
uint64 seed = luax_checkrandomseed(L, 1);
t->randomseed(seed);
}
t->setState(luax_checkrandomstate(L, 1));
luax_newtype(L, "RandomGenerator", MATH_RANDOM_GENERATOR_T, (void *) t);
return 1;
@@ -264,9 +261,10 @@ int w_noise(lua_State *L)
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "randomseed", w_randomseed },
{ "setRandomState", w_setRandomState },
{ "getRandomState", w_getRandomState },
{ "random", w_random },
{ "randomnormal", w_randomnormal },
{ "randomNormal", w_randomNormal },
{ "newRandomGenerator", w_newRandomGenerator },
{ "newBezierCurve", w_newBezierCurve },
{ "triangulate", w_triangulate },
@@ -285,6 +283,7 @@ static const lua_CFunction types[] =
extern "C" int luaopen_love_math(lua_State *L)
{
Math::instance.retain();
WrappedModule w;
w.module = &Math::instance;
w.name = "math";