diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-18 11:40:51 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-18 11:40:51 -0300 |
commit | 2a03170ebd096288c833e034000dc177784f2040 (patch) | |
tree | b2d407c549496097e8e8161c9d47aaa126509a42 | |
parent | 05f55cc062739f0a0765882904b9f30d7073ef85 (diff) | |
download | lua-2a03170ebd096288c833e034000dc177784f2040.tar.gz lua-2a03170ebd096288c833e034000dc177784f2040.tar.bz2 lua-2a03170ebd096288c833e034000dc177784f2040.zip |
random(0) and random(x,0) are wrong (0 is read as no argument!).
-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 | ||