-math.randomseed( os.time() )
+-- compatibility shim
+math.random = function(...)
+ local arg = {...};
+ if (#arg == 1) then
+ return 1 + nh.rn2(arg[1]);
+ elseif (#arg == 2) then
+ return nh.random(arg[1], arg[2] + 1 - arg[1]);
+ else
+ -- we don't support reals
+ error("NetHack math.random requires at least one parameter");
+ end
+end
function shuffle(list)
for i = #list, 2, -1 do
nh.pline("Message text to show.");
+=== random
+
+Generate a random number.
+
+Example:
+
+ nh.random(10); -- returns a number between 0 and 9, inclusive.
+ nh.random(1,5); -- same as 1 + nh.random(5);
+
+=== rn2
+
+Generate a random number.
+
+Example:
+
+ nh.rn2(10); -- returns a number between 0 and 9, inclusive.
+
=== s_suffix
Return a string converted to possessive.
static int FDECL(nhl_s_suffix, (lua_State *));
static int FDECL(nhl_ing_suffix, (lua_State *));
static int FDECL(nhl_an, (lua_State *));
+static int FDECL(nhl_rn2, (lua_State *));
+static int FDECL(nhl_random, (lua_State *));
static int FDECL(nhl_meta_u_index, (lua_State *));
static int FDECL(nhl_meta_u_newindex, (lua_State *));
static int FDECL(nhl_u_clear_inventory, (lua_State *));
return 1;
}
+/* rn2(10) */
+static int
+nhl_rn2(L)
+lua_State *L;
+{
+ int argc = lua_gettop(L);
+
+ if (argc == 1)
+ lua_pushinteger(L, rn2(luaL_checkinteger(L, 1)));
+ else
+ nhl_error(L, "Wrong args");
+
+ return 1;
+}
+
+/* random(10); -- is the same as rn2(10); */
+/* random(5,8); -- same as 5 + rn2(8); */
+static int
+nhl_random(L)
+lua_State *L;
+{
+ int argc = lua_gettop(L);
+
+ if (argc == 1)
+ lua_pushinteger(L, rn2(luaL_checkinteger(L, 1)));
+ else if (argc == 2)
+ lua_pushinteger(L, luaL_checkinteger(L, 1) + rn2(luaL_checkinteger(L, 2)));
+ else
+ nhl_error(L, "Wrong args");
+
+ return 1;
+}
+
/* get mandatory integer value from table */
int
get_table_int(L, name)
{"s_suffix", nhl_s_suffix},
{"ing_suffix", nhl_ing_suffix},
{"an", nhl_an},
+ {"rn2", nhl_rn2},
+ {"random", nhl_random},
{NULL, NULL}
};