Expose core random number functions to lua

Expose nh.rn2() and nh.random() to lua.
Add a math.random() compatibility shim to nhlib.lua
This commit is contained in:
Pasi Kallinen
2020-03-25 12:24:02 +02:00
parent c8fb419a2c
commit eec9c2e209
3 changed files with 66 additions and 1 deletions

View File

@@ -1,5 +1,16 @@
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