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

Function math_random

third-party/lua-5.5.0/src/lmathlib.c:582–614  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

580
581
582static 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
617static 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