diff options
| author | ame <[email protected]> | 2026-06-29 05:48:58 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2026-06-29 05:48:58 -0500 |
| commit | 727d3541a122795b405208242e6fdf7f8b5add07 (patch) | |
| tree | db049f349f669d37c6bf7d7feea18481a4f8b2d9 /src | |
| parent | 4ecdd3c18ccc5d3bcaa82ed720bf28443aa0ca9d (diff) | |
:detatch fix and math.random
Diffstat (limited to 'src')
| -rw-r--r-- | src/math.c | 17 | ||||
| -rw-r--r-- | src/math.h | 2 | ||||
| -rw-r--r-- | src/reg.c | 2 | ||||
| -rw-r--r-- | src/thread.c | 2 |
4 files changed, 22 insertions, 1 deletions
@@ -46,3 +46,20 @@ int l_lcm(lua_State* L){ free(nums); return 1; } + +int l_random(lua_State* L){ + size_t min = 1; + switch(lua_gettop(L)){ + case 2: + min = lua_tointeger(L, 1); + lua_remove(L, 1); + case 1: + lua_pushinteger(L, (rand() % (lua_tointeger(L, 1) - min + 1)) + min); + break; + case 0: + default: + lua_pushnumber(L, (double)rand() / (double)RAND_MAX); + } + + return 1; +} @@ -3,11 +3,13 @@ int l_lcm(lua_State*); int l_gcd(lua_State*); +int l_random(lua_State*); #define clean_lullaby_math luaI_nothing static const luaL_Reg math_function_list [] = { {"lcm",l_lcm}, + {"random",l_random}, //{"gcd",l_gcd}, {NULL,NULL} @@ -8,6 +8,7 @@ #include "test.h"
#include "config.h"
#include "lullaby.h"
+#include <time.h>
#define open_common(name, config)\
int luaopen_lullaby_##name (lua_State* L){\
@@ -38,6 +39,7 @@ lua_settable(L, T); int luaopen_lullaby(lua_State* L) {
+ srand(time(NULL));
lua_newtable(L);
int top = lua_gettop(L);
diff --git a/src/thread.c b/src/thread.c index cd2eb5f..f34f314 100644 --- a/src/thread.c +++ b/src/thread.c @@ -213,7 +213,7 @@ int _thread_detach(lua_State* L){ lua_pushstring(L, "_");
lua_gettable(L, 1);
struct thread_info* info = lua_touserdata(L, -1);
- lua_getmetatable(L, -1);
+ lua_getmetatable(L, 1);
int idx = lua_gettop(L);
luaI_tsetnil(L, idx, "__gc");
|
