MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / math_random

Function math_random

extlibs/lua/src/lmathlib.c:556–588  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

554
555
556static 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
591static void setseed (lua_State *L, Rand64 *state,

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected