aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2012-03-28 21:16:47 +0300
committerPerttu Ahola <celeron55@gmail.com>2012-03-28 22:01:23 +0300
commit68625b047f1fdebba747a83630163af99faf17e3 (patch)
treec5e3b6ca815c443689fd86bdb3af2c880dbbdd1e /src
parent9a1df7bf3879bc77025779f11f4a99943572931f (diff)
downloadminetest-68625b047f1fdebba747a83630163af99faf17e3.tar.gz
minetest-68625b047f1fdebba747a83630163af99faf17e3.tar.bz2
minetest-68625b047f1fdebba747a83630163af99faf17e3.zip
Add range option to PseudoRandom:next()
Diffstat (limited to 'src')
-rw-r--r--src/scriptapi.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index 944b81a5e..70ca6c86a 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -3157,12 +3157,23 @@ private:
return 0;
}
- // next(self) -> get next value
+ // next(self, min=0, max=32767) -> get next value
static int l_next(lua_State *L)
{
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;
- lua_pushinteger(L, pseudo.next());
+ int val = pseudo.next();
+ val = (val % (max-min+1)) + min;
+ lua_pushinteger(L, val);
return 1;
}