mirror of
https://github.com/lua/lua
synced 2024-11-22 12:51:30 +03:00
random(0) and random(x,0) are wrong (0 is read as no argument!).
This commit is contained in:
parent
05f55cc062
commit
2a03170ebd
8
bugs
8
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)
|
||||
|
19
lmathlib.c
19
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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user