mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
Add range option to PseudoRandom:next()
This commit is contained in:
parent
9a1df7bf38
commit
68625b047f
@ -632,7 +632,10 @@ methods:
|
|||||||
PseudoRandom: A pseudorandom number generator
|
PseudoRandom: A pseudorandom number generator
|
||||||
- Can be created via PseudoRandom(seed)
|
- Can be created via PseudoRandom(seed)
|
||||||
methods:
|
methods:
|
||||||
- next(): return next random number [0...32767]
|
- next(): return next integer random number [0...32767]
|
||||||
|
- next(min, max): return next integer random number [min...max]
|
||||||
|
(max - min) must be 32767 or <= 6553 due to the simple
|
||||||
|
implementation making bad distribution otherwise.
|
||||||
|
|
||||||
Registered entities
|
Registered entities
|
||||||
--------------------
|
--------------------
|
||||||
|
@ -3157,12 +3157,23 @@ private:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// next(self) -> get next value
|
// next(self, min=0, max=32767) -> get next value
|
||||||
static int l_next(lua_State *L)
|
static int l_next(lua_State *L)
|
||||||
{
|
{
|
||||||
LuaPseudoRandom *o = checkobject(L, 1);
|
LuaPseudoRandom *o = checkobject(L, 1);
|
||||||
|
int min = 0;
|
||||||
|
int max = 32767;
|
||||||
|
lua_settop(L, 3); // Fill 2 and 3 with nil if they don't exist
|
||||||
|
if(!lua_isnil(L, 2))
|
||||||
|
min = luaL_checkinteger(L, 2);
|
||||||
|
if(!lua_isnil(L, 3))
|
||||||
|
max = luaL_checkinteger(L, 3);
|
||||||
|
if(max - min != 32767 && max - min > 32767/5)
|
||||||
|
throw LuaError(L, "PseudoRandom.next() max-min is not 32767 and is > 32768/5. This is disallowed due to the bad random distribution the implementation would otherwise make.");
|
||||||
PseudoRandom &pseudo = o->m_pseudo;
|
PseudoRandom &pseudo = o->m_pseudo;
|
||||||
lua_pushinteger(L, pseudo.next());
|
int val = pseudo.next();
|
||||||
|
val = (val % (max-min+1)) + min;
|
||||||
|
lua_pushinteger(L, val);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user