random(0) and random(x,0) are wrong (0 is read as no argument!).

This commit is contained in:
Roberto Ierusalimschy 1999-08-18 11:40:51 -03:00
parent 05f55cc062
commit 2a03170ebd
2 changed files with 19 additions and 8 deletions

8
bugs
View File

@ -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)

View File

@ -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 */
}
}