From caa987faad6191a2abaf9d2741216ba619359fdb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Jul 1997 19:15:18 -0300 Subject: "random" accepts an 'n' to return between 1 and 'n'. --- mathlib.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'mathlib.c') diff --git a/mathlib.c b/mathlib.c index bb33db08..3be26b67 100644 --- a/mathlib.c +++ b/mathlib.c @@ -3,7 +3,7 @@ ** Mathematics library to LUA */ -char *rcs_mathlib="$Id: mathlib.c,v 1.24 1997/06/09 17:30:10 roberto Exp roberto $"; +char *rcs_mathlib="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp roberto $"; #include #include @@ -171,7 +171,11 @@ static void math_rad (void) static void math_random (void) { - lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX); + double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; + if (lua_getparam(1) == LUA_NOOBJECT) + lua_pushnumber(r); + else + lua_pushnumber((int)(r*luaL_check_number(1)) + 1); } static void math_randomseed (void) -- cgit v1.2.3-55-g6feb