From c41c94eaaa71425c2bd2d076566923a7013c23a3 Mon Sep 17 00:00:00 2001 From: The Lord of Owls <119661891+The-Lord-of-Owls@users.noreply.github.com> Date: Sun, 11 Dec 2022 13:38:15 -0500 Subject: [PATCH 1/2] Updated random.Number, added random.WholeNumber Added the option to provide a single argument to the random.Number function. Behavior should mimic math.Random when provided a single argument. Purpose: Allowing math.random to be replaced with random.Number interchangeably without running into any annoying issues. Also added a new function random.WholeNumber, same functionality as random.Number only it does not give a decimal --- src/main.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 71c258a..3629ae8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,13 +7,26 @@ std::random_device rd; std::mt19937 gen; int randomNumber(lua_State* state) { - if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) { - LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER); - LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER); + if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) { //Check if we have the first argument + LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER); //Verify the first argument is a number - std::uniform_real_distribution dist(LUA->GetNumber(1), LUA->GetNumber(2)); - LUA->PushNumber(dist(gen)); + if (LUA->GetType(2) != GarrysMod::Lua::Type::NIL) { //Check if we have the second argument + LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER); //Verify the second argument is a number + + //Push our random number between the first and second argument + std::uniform_real_distribution dist(LUA->GetNumber(1), LUA->GetNumber(2)); + LUA->PushNumber(dist(gen)); + } else { + //Added in by The Owl Cafe + //Purpose: To make it behave fully like the math.random function in glua, effectively allowing it to fully replace math.random + + //We only have the first argument + //We return a number between 1 and the second argument + std::uniform_real_distribution dist( 1.0, LUA->GetNumber(1)); + LUA->PushNumber(dist(gen)); + } } else { + //Return a random number between 0 and 1 std::uniform_real_distribution dist; LUA->PushNumber(dist(gen)); } @@ -21,6 +34,34 @@ int randomNumber(lua_State* state) { return 1; } +int randomWholeNumber(lua_State* state) { + if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) { //Check if we have the first argument + LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER); //Verify the first argument is a number + + if (LUA->GetType(2) != GarrysMod::Lua::Type::NIL) { //Check if we have the second argument + LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER); //Verify the second argument is a number + + //Push our random number between the first and second argument + std::uniform_real_distribution dist(LUA->GetNumber(1), LUA->GetNumber(2)); + LUA->PushNumber(dist(gen)); + } else { + //Added in by The Owl Cafe + //Purpose: To make it behave fully like the math.random function in glua, effectively allowing it to fully replace math.random + + //We only have the first argument + //We return a number between 1 and the second argument + std::uniform_real_distribution dist( 1, LUA->GetNumber(1)); + LUA->PushNumber(dist(gen)); + } + } else { + //Return a random number between 0 and 1 + std::uniform_real_distribution dist; + LUA->PushNumber(dist(gen)); + } + + return 1; +} + int randomBytes(lua_State* state) { LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER); const double requestedSize = LUA->GetNumber(1); @@ -51,6 +92,10 @@ GMOD_MODULE_OPEN() { LUA->CreateTable(); LUA->PushCFunction(randomNumber); LUA->SetField(-2, "Number"); + + //Same as random.Number, but should provide a whole number instead of a decimal + LUA->PushCFunction(randomWholeNumber); + LUA->SetField(-2, "WholeNumber"); LUA->PushCFunction(randomBytes); LUA->SetField(-2, "Bytes"); @@ -61,4 +106,4 @@ GMOD_MODULE_OPEN() { GMOD_MODULE_CLOSE() { return 0; -} \ No newline at end of file +} From 9489296029c9baa4116aa42b3c112b5dd41ebbb8 Mon Sep 17 00:00:00 2001 From: The Lord of Owls <119661891+The-Lord-of-Owls@users.noreply.github.com> Date: Sun, 11 Dec 2022 15:11:19 -0500 Subject: [PATCH 2/2] Slight fix to random.WholeNumber --- src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3629ae8..b148b03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,7 +42,7 @@ int randomWholeNumber(lua_State* state) { LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER); //Verify the second argument is a number //Push our random number between the first and second argument - std::uniform_real_distribution dist(LUA->GetNumber(1), LUA->GetNumber(2)); + std::uniform_int_distribution dist(LUA->GetNumber(1), LUA->GetNumber(2)); LUA->PushNumber(dist(gen)); } else { //Added in by The Owl Cafe @@ -50,12 +50,12 @@ int randomWholeNumber(lua_State* state) { //We only have the first argument //We return a number between 1 and the second argument - std::uniform_real_distribution dist( 1, LUA->GetNumber(1)); + std::uniform_int_distribution dist( 1, LUA->GetNumber(1)); LUA->PushNumber(dist(gen)); } } else { //Return a random number between 0 and 1 - std::uniform_real_distribution dist; + std::uniform_int_distribution dist; LUA->PushNumber(dist(gen)); }