Browse Source

ensure proper rounding

v5-2
Roberto Ierusalimschy 23 years ago
parent
commit
00a40f4d6a
  1. 6
      lmathlib.c

6
lmathlib.c

@ -1,5 +1,5 @@
/*
** $Id: lmathlib.c,v 1.47 2002/06/18 15:16:18 roberto Exp roberto $
** $Id: lmathlib.c,v 1.48 2002/06/24 13:54:13 roberto Exp roberto $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
@ -177,14 +177,14 @@ static int math_random (lua_State *L) {
case 1: { /* only upper limit */
int u = luaL_check_int(L, 1);
luaL_arg_check(L, 1<=u, 1, "interval is empty");
lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */
break;
}
case 2: { /* lower and upper limits */
int l = luaL_check_int(L, 1);
int u = luaL_check_int(L, 2);
luaL_arg_check(L, l<=u, 2, "interval is empty");
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
break;
}
default: return luaL_error(L, "wrong number of arguments");

Loading…
Cancel
Save