Skip to content

Commit

Permalink
Fix Lua PseudoRandom seeds being mangled
Browse files Browse the repository at this point in the history
closes #14237
  • Loading branch information
sfan5 committed Jan 14, 2024
1 parent e83530d commit d20f118
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions games/devtest/mods/unittests/misc.lua
@@ -1,8 +1,11 @@
local function test_random()
-- Try out PseudoRandom
local pseudo = PseudoRandom(13)
assert(pseudo:next() == 22290)
assert(pseudo:next() == 13854)
-- We have comprehensive unit tests in C++, this is just to make sure the API code isn't messing up
local pr = PseudoRandom(13)
assert(pr:next() == 22290)
assert(pr:next() == 13854)

local pr2 = PseudoRandom(-101)
assert(pr2:next(0, 100) == 35)
end
unittests.register("test_random", test_random)

Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_noise.cpp
Expand Up @@ -433,7 +433,7 @@ int LuaPseudoRandom::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

u64 seed = luaL_checknumber(L, 1);
s32 seed = luaL_checkinteger(L, 1);
LuaPseudoRandom *o = new LuaPseudoRandom(seed);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
Expand Down

0 comments on commit d20f118

Please sign in to comment.