mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
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:
@@ -27,17 +27,27 @@ namespace love
|
||||
namespace math
|
||||
{
|
||||
|
||||
uint64 luax_checkrandomseed(lua_State *L, int idx)
|
||||
RandomGenerator::State luax_checkrandomstate(lua_State *L, int idx)
|
||||
{
|
||||
union
|
||||
RandomGenerator::State s;
|
||||
|
||||
if (!lua_isnoneornil(L, idx + 1))
|
||||
{
|
||||
double seed_double;
|
||||
uint64 seed_uint;
|
||||
} s;
|
||||
uint32 low = (uint32) luaL_checknumber(L, idx);
|
||||
uint32 high = (uint32) luaL_checknumber(L, idx + 1);
|
||||
|
||||
s.seed_double = luaL_checknumber(L, idx);
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
s.b32.a = high;
|
||||
s.b32.b = low;
|
||||
#else
|
||||
s.b32.b = high;
|
||||
s.b32.a = low;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
s.b64 = (uint64) luaL_checknumber(L, idx);
|
||||
|
||||
return s.seed_uint;
|
||||
return s;
|
||||
}
|
||||
|
||||
int luax_getrandom(lua_State *L, int startidx, double r)
|
||||
@@ -71,45 +81,49 @@ RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx)
|
||||
return luax_checktype<RandomGenerator>(L, idx, "RandomGenerator", MATH_RANDOM_GENERATOR_T);
|
||||
}
|
||||
|
||||
int w_RandomGenerator_randomseed(lua_State *L)
|
||||
int w_RandomGenerator_setState(lua_State *L)
|
||||
{
|
||||
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
|
||||
uint64 seed = luax_checkrandomseed(L, 2);
|
||||
rng->randomseed(seed);
|
||||
rng->setState(luax_checkrandomstate(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_RandomGenerator_getState(lua_State *L)
|
||||
{
|
||||
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
|
||||
|
||||
uint32 low = 0, high = 0;
|
||||
rng->getState(low, high);
|
||||
|
||||
lua_pushnumber(L, (lua_Number) low);
|
||||
lua_pushnumber(L, (lua_Number) high);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_RandomGenerator_random(lua_State *L)
|
||||
{
|
||||
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
|
||||
return luax_getrandom(L, 2, rng->random());
|
||||
}
|
||||
|
||||
int w_RandomGenerator_randomnormal(lua_State *L)
|
||||
int w_RandomGenerator_randomNormal(lua_State *L)
|
||||
{
|
||||
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
|
||||
|
||||
double mean = 0.0, stddev = 1.0;
|
||||
if (lua_gettop(L) > 2)
|
||||
{
|
||||
mean = luaL_checknumber(L, 2);
|
||||
stddev = luaL_checknumber(L, 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
stddev = luaL_optnumber(L, 2, 1.0);
|
||||
}
|
||||
double stddev = luaL_optnumber(L, 2, 1.0);
|
||||
double mean = luaL_optnumber(L, 3, 0.0);
|
||||
double r = rng->randomNormal(stddev);
|
||||
|
||||
double r = rng->randomnormal(stddev);
|
||||
lua_pushnumber(L, r + mean);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "randomseed", w_RandomGenerator_randomseed },
|
||||
{ "setState", w_RandomGenerator_setState },
|
||||
{ "getState", w_RandomGenerator_getState },
|
||||
{ "random", w_RandomGenerator_random },
|
||||
{ "randomnormal", w_RandomGenerator_randomnormal },
|
||||
{ "randomNormal", w_RandomGenerator_randomNormal },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user