Skip to content

Commit

Permalink
Apply review.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfence committed Dec 28, 2023
1 parent 4d7be2c commit b043ad0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 71 deletions.
8 changes: 4 additions & 4 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5279,8 +5279,8 @@ Utilities
-- acceleration_default, acceleration_air (5.8.0)
physics_overrides_v2 = true,
-- PseudoRandom and PcgRandom state is restorable
-- PseudoRandom have get_state method
-- PcgRandom have get_state and set_state methods
-- PseudoRandom has get_state method
-- PcgRandom has get_state and set_state methods (5.9.0)
random_state_restore = true,
}
```
Expand Down Expand Up @@ -8050,8 +8050,8 @@ It can be created via `PcgRandom(seed)` or `PcgRandom(seed, sequence)`.
* `mean = (max - min) / 2`, and
* `variance = (((max - min + 1) ^ 2) - 1) / (12 * num_trials)`
* Increasing `num_trials` improves accuracy of the approximation
* `get_state()`: return pseudorandom generator state encoded in string
* `set_state(state_string)`: restore pseudorandom generator state from encoded string
* `get_state()`: return generator state encoded in string
* `set_state(state_string)`: restore generator state from encoded string

`PerlinNoise`
-------------
Expand Down
1 change: 0 additions & 1 deletion games/devtest/mods/unittests/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ dofile(modpath .. "/get_version.lua")
dofile(modpath .. "/itemstack_equals.lua")
dofile(modpath .. "/content_ids.lua")
dofile(modpath .. "/metadata.lua")
dofile(modpath .. "/random.lua")

--------------

Expand Down
37 changes: 31 additions & 6 deletions games/devtest/mods/unittests/misc.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
local function test_random()
-- Try out PseudoRandom
local pseudo = PseudoRandom(13)
assert(pseudo:next() == 22290)
assert(pseudo:next() == 13854)
local function test_pseudo_random()
local gen1 = PseudoRandom(13)
assert(gen1:next() == 22290)
assert(gen1:next() == 13854)

for n=2,128 do
gen1:next()
end

local gen2 = PseudoRandom(gen1:get_state())

for n=128,256 do
assert(gen1:next()==gen2:next())
end
end
unittests.register("test_pseudo_random", test_pseudo_random)

local function test_pcg_random()
local gen1 = PcgRandom(55)

for n=0,128 do
gen1:next()
end

local gen2 = PcgRandom(26)
gen2:set_state(gen1:get_state())

for n=128,256 do
assert(gen1:next()==gen2:next())
end
end
unittests.register("test_random", test_random)
unittests.register("test_pcg_random", test_pcg_random)

local function test_dynamic_media(cb, player)
if core.get_player_information(player:get_player_name()).protocol_version < 40 then
Expand Down
35 changes: 0 additions & 35 deletions games/devtest/mods/unittests/random.lua

This file was deleted.

12 changes: 6 additions & 6 deletions src/noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials)
return myround((float)accum / num_trials);
}

void PcgRandom::getState(u64 &state, u64 &inc)
void PcgRandom::getState(u64 state[2]) const
{
state = m_state;
inc = m_inc;
state[0] = m_state;
state[1] = m_inc;
}

void PcgRandom::setState(u64 state, u64 inc)
void PcgRandom::setState(const u64 state[2])
{
m_state = state;
m_inc = inc;
m_state = state[0];
m_inc = state[1];
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions src/noise.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PseudoRandom {
}

// Allow save and restore of state
inline int getState()
inline int getState() const
{
return m_next;
}
Expand All @@ -100,8 +100,8 @@ class PcgRandom {
s32 randNormalDist(s32 min, s32 max, int num_trials=6);

// Allow save and restore of state
void getState(u64 &state, u64 &inc);
void setState(u64 state, u64 inc);
void getState(u64 state[2]) const;
void setState(const u64 state[2]);
private:
u64 m_state;
u64 m_inc;
Expand Down
22 changes: 10 additions & 12 deletions src/script/lua_api/l_noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ int LuaPcgRandom::l_get_state(lua_State *L)

LuaPcgRandom *o = checkObject<LuaPcgRandom>(L, 1);

u64 state, inc;
o->m_rnd.getState(state, inc);
u64 state[2];
o->m_rnd.getState(state);

std::stringstream s_state;

s_state << std::hex << std::setw(64) << std::setfill('0') << state;
s_state << std::hex << std::setw(64) << std::setfill('0') << inc;
s_state << std::hex << std::setw(64) << std::setfill('0') << state[0];
s_state << std::hex << std::setw(64) << std::setfill('0') << state[1];

lua_pushstring(L, s_state.str().c_str());
return 1;
Expand All @@ -535,24 +535,22 @@ int LuaPcgRandom::l_set_state(lua_State *L)

LuaPcgRandom *o = checkObject<LuaPcgRandom>(L, 1);
if (!lua_isstring(L, 2)) {
errorstream<<"PcgRandom.set_state(): Expected hex strings"<<std::endl;
throw LuaError("PcgRandom.set_state(): Expected hex strings");
}

std::string l_string = lua_tostring(L, 2);
if (l_string.size()!=128) {
errorstream<<"PcgRandom.set_state(): Expected hex strings of 128 characters"<<std::endl;
throw LuaError("PcgRandom.set_state(): Expected hex strings of 128 characters");
}

std::istringstream s_state(l_string.substr(0, 64));
std::istringstream s_inc(l_string.substr(64, 64));
std::istringstream s_state_0(l_string.substr(0, 64));
std::istringstream s_state_1(l_string.substr(64, 64));

u64 state, inc;
s_state >> std::hex >> state;
s_inc >> std::hex >> inc;
u64 state[2];
s_state_0 >> std::hex >> state[0];
s_state_1 >> std::hex >> state[1];

o->m_rnd.setState(state, inc);
o->m_rnd.setState(state);

return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions src/unittest/test_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ void TestRandom::testPcgRandom()
for (u32 i = 0; i != 128; i++)
UASSERTEQ(u32, pr.next(), expected_pcgrandom_results[i]);

PcgRandom pr2(814538, 998877);
u64 state, inc;
pr.getState(state, inc);
pr2.setState(state, inc);
PcgRandom pr2(0, 0);
u64 state[2];
pr.getState(state);
pr2.setState(state);

for (u32 i = 128; i != 256; i++) {
UASSERTEQ(u32, pr.next(), expected_pcgrandom_results[i]);
Expand Down

0 comments on commit b043ad0

Please sign in to comment.