| 554 | |
| 555 | |
| 556 | static int math_random (lua_State *L) { |
| 557 | lua_Integer low, up; |
| 558 | lua_Unsigned p; |
| 559 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); |
| 560 | Rand64 rv = nextrand(state->s); /* next pseudo-random value */ |
| 561 | switch (lua_gettop(L)) { /* check number of arguments */ |
| 562 | case 0: { /* no arguments */ |
| 563 | lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ |
| 564 | return 1; |
| 565 | } |
| 566 | case 1: { /* only upper limit */ |
| 567 | low = 1; |
| 568 | up = luaL_checkinteger(L, 1); |
| 569 | if (up == 0) { /* single 0 as argument? */ |
| 570 | lua_pushinteger(L, I2UInt(rv)); /* full random integer */ |
| 571 | return 1; |
| 572 | } |
| 573 | break; |
| 574 | } |
| 575 | case 2: { /* lower and upper limits */ |
| 576 | low = luaL_checkinteger(L, 1); |
| 577 | up = luaL_checkinteger(L, 2); |
| 578 | break; |
| 579 | } |
| 580 | default: return luaL_error(L, "wrong number of arguments"); |
| 581 | } |
| 582 | /* random integer in the interval [low, up] */ |
| 583 | luaL_argcheck(L, low <= up, 1, "interval is empty"); |
| 584 | /* project random integer into the interval [0, up - low] */ |
| 585 | p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); |
| 586 | lua_pushinteger(L, p + (lua_Unsigned)low); |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | static void setseed (lua_State *L, Rand64 *state, |
nothing calls this directly
no test coverage detected