fix: missing Random:seed method

This commit is contained in:
MihailRis 2025-09-26 00:25:00 +03:00
parent 1f3b7f828d
commit 1350910d28
3 changed files with 21 additions and 6 deletions

View File

@ -674,6 +674,9 @@ function __vc_create_random_methods(random_methods)
local buffer = nil
local buffer_size = 64
local seed_func = random_methods.seed
local random_func = random_methods.random
function random_methods:bytes(n)
local bytes = Bytearray(n)
for i=1,n do
@ -682,9 +685,14 @@ function __vc_create_random_methods(random_methods)
return bytes
end
function random_methods:seed(x)
seed_func(self, x)
buffer = nil
end
function random_methods:random(a, b)
if not buffer or index > #buffer then
buffer = self:generate(buffer_size)
buffer = random_func(self, buffer_size)
index = 1
end
local value = buffer[index]

View File

@ -22,7 +22,7 @@ static int l_random(lua::State* L) {
}
}
static int l_generate(lua::State* L) {
static int l_bytes(lua::State* L) {
size_t size = lua::tointeger(L, 1);
auto randomEngine = util::seeded_random_engine(random_device);
@ -40,7 +40,7 @@ static int l_uuid(lua::State* L) {
const luaL_Reg randomlib[] = {
{"random", lua::wrap<l_random>},
{"bytes", lua::wrap<l_generate>},
{"bytes", lua::wrap<l_bytes>},
{"uuid", lua::wrap<l_uuid>},
{NULL, NULL}
};

View File

@ -6,7 +6,7 @@
using namespace lua;
using namespace std::chrono;
static int l_generate(lua::State* L) {
static int l_random(lua::State* L) {
std::uniform_int_distribution<> dist(0, std::numeric_limits<int>::max());
auto& rng = require_userdata<LuaRandom>(L, 1).rng;
@ -20,6 +20,11 @@ static int l_generate(lua::State* L) {
return 1;
}
static int l_seed(lua::State* L) {
require_userdata<LuaRandom>(L, 1).rng = std::mt19937(lua::touinteger(L, 2));
return 0;
}
static int l_meta_meta_call(lua::State* L) {
integer_t seed;
if (lua::isnoneornil(L, 1)) {
@ -35,8 +40,10 @@ int LuaRandom::createMetatable(lua::State* L) {
requireglobal(L, "__vc_create_random_methods");
createtable(L, 0, 0);
pushcfunction(L, wrap<l_generate>);
setfield(L, "generate");
pushcfunction(L, wrap<l_random>);
setfield(L, "random");
pushcfunction(L, wrap<l_seed>);
setfield(L, "seed");
call(L, 1, 1);
setfield(L, "__index");