The following implementation is the one shipped with Lua itself but with * rand() replaced by redisLrand48(). */
| 1362 | /* The following implementation is the one shipped with Lua itself but with |
| 1363 | * rand() replaced by redisLrand48(). */ |
| 1364 | int redis_math_random (lua_State *L) { |
| 1365 | /* the `%' avoids the (rare) case of r==1, and is needed also because on |
| 1366 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ |
| 1367 | lua_Number r = (lua_Number)(redisLrand48()%REDIS_LRAND48_MAX) / |
| 1368 | (lua_Number)REDIS_LRAND48_MAX; |
| 1369 | switch (lua_gettop(L)) { /* check number of arguments */ |
| 1370 | case 0: { /* no arguments */ |
| 1371 | lua_pushnumber(L, r); /* Number between 0 and 1 */ |
| 1372 | break; |
| 1373 | } |
| 1374 | case 1: { /* only upper limit */ |
| 1375 | int u = luaL_checkint(L, 1); |
| 1376 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); |
| 1377 | lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ |
| 1378 | break; |
| 1379 | } |
| 1380 | case 2: { /* lower and upper limits */ |
| 1381 | int l = luaL_checkint(L, 1); |
| 1382 | int u = luaL_checkint(L, 2); |
| 1383 | luaL_argcheck(L, l<=u, 2, "interval is empty"); |
| 1384 | lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ |
| 1385 | break; |
| 1386 | } |
| 1387 | default: return luaL_error(L, "wrong number of arguments"); |
| 1388 | } |
| 1389 | return 1; |
| 1390 | } |
| 1391 | |
| 1392 | int redis_math_randomseed (lua_State *L) { |
| 1393 | redisSrand48(luaL_checkint(L, 1)); |
nothing calls this directly
no test coverage detected