MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / math_random

Function math_random

third-party/lua-5.4.6/src/lmathlib.c:574–606  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 11

lua_upvalueindexFunction · 0.85
lua_touserdataFunction · 0.70
nextrandFunction · 0.70
lua_gettopFunction · 0.70
lua_pushnumberFunction · 0.70
I2dFunction · 0.70
luaL_checkintegerFunction · 0.70
lua_pushintegerFunction · 0.70
I2UIntFunction · 0.70
luaL_errorFunction · 0.70
projectFunction · 0.70

Tested by

no test coverage detected