From 2a03170ebd096288c833e034000dc177784f2040 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Aug 1999 11:40:51 -0300 Subject: [PATCH] random(0) and random(x,0) are wrong (0 is read as no argument!). --- bugs | 8 ++++++++ lmathlib.c | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/bugs b/bugs index cf25945d..7fea0b7e 100644 --- a/bugs +++ b/bugs @@ -102,3 +102,11 @@ Wed Jun 16 10:32:46 EST 1999 the number of returns of a function. (since 3.1) + + +--- Version 3.2 + +** lmathlib.c +Wed Aug 18 11:28:38 EST 1999 +>> random(0) and random(x,0) are wrong (0 is read as no argument!). +(by Dave Bollinger; since 3.1) diff --git a/lmathlib.c b/lmathlib.c index 0ea8491e..b6eb16fb 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.17 1999/07/07 17:54:08 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.18 1999/08/16 20:52:00 roberto Exp roberto $ ** Lua standard mathematical library ** See Copyright Notice in lua.h */ @@ -144,17 +144,20 @@ static void math_random (void) { /* the '%' avoids the (rare) case of r==1, and is needed also because on some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */ double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; - int l = luaL_opt_int(1, 0); - if (l == 0) - lua_pushnumber(r); + if (lua_getparam(1) == LUA_NOOBJECT) /* no arguments? */ + lua_pushnumber(r); /* real between 0 & 1 */ else { - int u = luaL_opt_int(2, 0); - if (u == 0) { - u = l; + int l, u; /* lower & upper limits */ + if (lua_getparam(2) == LUA_NOOBJECT) { /* only one argument? */ l = 1; + u = luaL_check_int(1); + } + else { /* two arguments */ + l = luaL_check_int(1); + u = luaL_check_int(2); } luaL_arg_check(l<=u, 1, "interval is empty"); - lua_pushnumber((int)(r*(u-l+1))+l); + lua_pushnumber((int)(r*(u-l+1))+l); /* integer between l & u */ } }