MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / math_random

Function math_random

3rd/lua-5.4.3/src/lmathlib.c:557–589  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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