diff options
| -rw-r--r-- | bugs | 8 | ||||
| -rw-r--r-- | lmathlib.c | 19 |
2 files changed, 19 insertions, 8 deletions
| @@ -102,3 +102,11 @@ Wed Jun 16 10:32:46 EST 1999 | |||
| 102 | the number of returns of a function. | 102 | the number of returns of a function. |
| 103 | (since 3.1) | 103 | (since 3.1) |
| 104 | 104 | ||
| 105 | |||
| 106 | |||
| 107 | --- Version 3.2 | ||
| 108 | |||
| 109 | ** lmathlib.c | ||
| 110 | Wed Aug 18 11:28:38 EST 1999 | ||
| 111 | >> random(0) and random(x,0) are wrong (0 is read as no argument!). | ||
| 112 | (by Dave Bollinger; since 3.1) | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.17 1999/07/07 17:54:08 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.18 1999/08/16 20:52:00 roberto Exp roberto $ |
| 3 | ** Lua standard mathematical library | 3 | ** Lua standard mathematical library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -144,17 +144,20 @@ static void math_random (void) { | |||
| 144 | /* the '%' avoids the (rare) case of r==1, and is needed also because on | 144 | /* the '%' avoids the (rare) case of r==1, and is needed also because on |
| 145 | some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */ | 145 | some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */ |
| 146 | double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; | 146 | double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; |
| 147 | int l = luaL_opt_int(1, 0); | 147 | if (lua_getparam(1) == LUA_NOOBJECT) /* no arguments? */ |
| 148 | if (l == 0) | 148 | lua_pushnumber(r); /* real between 0 & 1 */ |
| 149 | lua_pushnumber(r); | ||
| 150 | else { | 149 | else { |
| 151 | int u = luaL_opt_int(2, 0); | 150 | int l, u; /* lower & upper limits */ |
| 152 | if (u == 0) { | 151 | if (lua_getparam(2) == LUA_NOOBJECT) { /* only one argument? */ |
| 153 | u = l; | ||
| 154 | l = 1; | 152 | l = 1; |
| 153 | u = luaL_check_int(1); | ||
| 154 | } | ||
| 155 | else { /* two arguments */ | ||
| 156 | l = luaL_check_int(1); | ||
| 157 | u = luaL_check_int(2); | ||
| 155 | } | 158 | } |
| 156 | luaL_arg_check(l<=u, 1, "interval is empty"); | 159 | luaL_arg_check(l<=u, 1, "interval is empty"); |
| 157 | lua_pushnumber((int)(r*(u-l+1))+l); | 160 | lua_pushnumber((int)(r*(u-l+1))+l); /* integer between l & u */ |
| 158 | } | 161 | } |
| 159 | } | 162 | } |
| 160 | 163 | ||
