| 580 | |
| 581 | |
| 582 | static int math_random (lua_State *L) { |
| 583 | lua_Integer low, up; |
| 584 | lua_Unsigned p; |
| 585 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); |
| 586 | Rand64 rv = nextrand(state->s); /* next pseudo-random value */ |
| 587 | switch (lua_gettop(L)) { /* check number of arguments */ |
| 588 | case 0: { /* no arguments */ |
| 589 | lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ |
| 590 | return 1; |
| 591 | } |
| 592 | case 1: { /* only upper limit */ |
| 593 | low = 1; |
| 594 | up = luaL_checkinteger(L, 1); |
| 595 | if (up == 0) { /* single 0 as argument? */ |
| 596 | lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */ |
| 597 | return 1; |
| 598 | } |
| 599 | break; |
| 600 | } |
| 601 | case 2: { /* lower and upper limits */ |
| 602 | low = luaL_checkinteger(L, 1); |
| 603 | up = luaL_checkinteger(L, 2); |
| 604 | break; |
| 605 | } |
| 606 | default: return luaL_error(L, "wrong number of arguments"); |
| 607 | } |
| 608 | /* random integer in the interval [low, up] */ |
| 609 | luaL_argcheck(L, low <= up, 1, "interval is empty"); |
| 610 | /* project random integer into the interval [0, up - low] */ |
| 611 | p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state); |
| 612 | lua_pushinteger(L, l_castU2S(p + l_castS2U(low))); |
| 613 | return 1; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | static void setseed (lua_State *L, Rand64 *state, |
nothing calls this directly
no test coverage detected