diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-13 16:32:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-13 16:32:52 -0300 |
| commit | 788b25115715340613d1e7139837a51cd429c000 (patch) | |
| tree | 1c776df52a1409325eab02c8ec03c2444d8f54a7 | |
| parent | 318575627f5a38328e38acf23733c53abfe65be1 (diff) | |
| download | lua-788b25115715340613d1e7139837a51cd429c000.tar.gz lua-788b25115715340613d1e7139837a51cd429c000.tar.bz2 lua-788b25115715340613d1e7139837a51cd429c000.zip | |
'math.random' operates with integers when used for integer results
(1 or 2 parameters)
| -rw-r--r-- | lmathlib.c | 20 |
1 files changed, 11 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.85 2013/05/06 17:22:55 roberto Exp $ | 2 | ** $Id: lmathlib.c,v 1.86 2013/05/26 13:45:24 roberto Exp roberto $ |
| 3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -204,26 +204,28 @@ static int math_random (lua_State *L) { | |||
| 204 | /* the `%' avoids the (rare) case of r==1, and is needed also because on | 204 | /* the `%' avoids the (rare) case of r==1, and is needed also because on |
| 205 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ | 205 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ |
| 206 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; | 206 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; |
| 207 | lua_Integer low, up; | ||
| 207 | switch (lua_gettop(L)) { /* check number of arguments */ | 208 | switch (lua_gettop(L)) { /* check number of arguments */ |
| 208 | case 0: { /* no arguments */ | 209 | case 0: { /* no arguments */ |
| 209 | lua_pushnumber(L, r); /* Number between 0 and 1 */ | 210 | lua_pushnumber(L, r); /* Number between 0 and 1 */ |
| 210 | break; | 211 | return 1; |
| 211 | } | 212 | } |
| 212 | case 1: { /* only upper limit */ | 213 | case 1: { /* only upper limit */ |
| 213 | lua_Number u = luaL_checknumber(L, 1); | 214 | low = 1; |
| 214 | luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); | 215 | up = luaL_checkinteger(L, 1); |
| 215 | lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ | ||
| 216 | break; | 216 | break; |
| 217 | } | 217 | } |
| 218 | case 2: { /* lower and upper limits */ | 218 | case 2: { /* lower and upper limits */ |
| 219 | lua_Number l = luaL_checknumber(L, 1); | 219 | low = luaL_checkinteger(L, 1); |
| 220 | lua_Number u = luaL_checknumber(L, 2); | 220 | up = luaL_checkinteger(L, 2); |
| 221 | luaL_argcheck(L, l <= u, 2, "interval is empty"); | ||
| 222 | lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ | ||
| 223 | break; | 221 | break; |
| 224 | } | 222 | } |
| 225 | default: return luaL_error(L, "wrong number of arguments"); | 223 | default: return luaL_error(L, "wrong number of arguments"); |
| 226 | } | 224 | } |
| 225 | /* random integer in the interval [low, up] */ | ||
| 226 | up++; /* change interval to [low, up) */ | ||
| 227 | luaL_argcheck(L, up - low > 0, 1, "interval is empty"); | ||
| 228 | lua_pushinteger(L, (lua_Integer)(r * (lua_Number)(up - low)) + low); | ||
| 227 | return 1; | 229 | return 1; |
| 228 | } | 230 | } |
| 229 | 231 | ||
