aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/readme.md4
-rw-r--r--library/lullaby/math.lua9
-rw-r--r--src/math.c17
-rw-r--r--src/math.h2
-rw-r--r--src/reg.c2
-rw-r--r--src/thread.c2
6 files changed, 35 insertions, 1 deletions
diff --git a/docs/readme.md b/docs/readme.md
index 83c02e5..c1f2bb0 100644
--- a/docs/readme.md
+++ b/docs/readme.md
@@ -2,6 +2,10 @@
> all files besides this and the content of net/ are out of date! ill be working on them later
+# dont use lua builtin random!!!!!!
+
+> lullaby provides its own random function llby.math.random(M?, N?). for now atleast you may run into issues using the builtin math.random when multithreading, this may affect some other lua utils
+
(name subject to change)
with the library in the same directory [(or one of the other valid search locations)](https://www.lua.org/pil/8.1.html)
diff --git a/library/lullaby/math.lua b/library/lullaby/math.lua
index 2914a56..487a3c5 100644
--- a/library/lullaby/math.lua
+++ b/library/lullaby/math.lua
@@ -7,4 +7,13 @@ local math = {}
---@return integer
function math.lcm(N) end
+---all results are inclusive
+---() : from 0.0 to 1.0
+---(M) : from 1 to M
+---(M, N) : from M to N
+---@param M integer?
+---@param N integer?
+---@return integer|number
+function math.random(M, N) end
+
return math
diff --git a/src/math.c b/src/math.c
index 3e36e3b..75cd578 100644
--- a/src/math.c
+++ b/src/math.c
@@ -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;
+}
diff --git a/src/math.h b/src/math.h
index 17e477e..7731831 100644
--- a/src/math.h
+++ b/src/math.h
@@ -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}
diff --git a/src/reg.c b/src/reg.c
index b90e03d..c57cdf7 100644
--- a/src/reg.c
+++ b/src/reg.c
@@ -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");