| 179 | |
| 180 | |
| 181 | static int math_random (lua_State *L) { |
| 182 | /* the `%' avoids the (rare) case of r==1, and is needed also because on |
| 183 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ |
| 184 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; |
| 185 | switch (lua_gettop(L)) { /* check number of arguments */ |
| 186 | case 0: { /* no arguments */ |
| 187 | lua_pushnumber(L, r); /* Number between 0 and 1 */ |
| 188 | break; |
| 189 | } |
| 190 | case 1: { /* only upper limit */ |
| 191 | int u = luaL_checkint(L, 1); |
| 192 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); |
| 193 | lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ |
| 194 | break; |
| 195 | } |
| 196 | case 2: { /* lower and upper limits */ |
| 197 | int l = luaL_checkint(L, 1); |
| 198 | int u = luaL_checkint(L, 2); |
| 199 | luaL_argcheck(L, l<=u, 2, "interval is empty"); |
| 200 | lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ |
| 201 | break; |
| 202 | } |
| 203 | default: return luaL_error(L, "wrong number of arguments"); |
| 204 | } |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static int math_randomseed (lua_State *L) { |
nothing calls this directly
no test coverage detected