mirror of https://github.com/rusefi/lua.git
new option for function "random": random(a,b) returns a<=x<=b
This commit is contained in:
parent
fe5c41fb8a
commit
05d89b5c05
24
lmathlib.c
24
lmathlib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lmathlib.c,v 1.11 1998/09/08 19:25:35 roberto Exp roberto $
|
** $Id: lmathlib.c,v 1.12 1998/12/28 13:44:54 roberto Exp roberto $
|
||||||
** Lua standard mathematical library
|
** Lua standard mathematical library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -153,16 +153,22 @@ static void math_max (void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_random (void)
|
static void math_random (void) {
|
||||||
{
|
/* the '%' is needed because on some systems (SunOS!) "rand()" may
|
||||||
/* the '%' is needed because on some systems (SunOS!) "rand()" may */
|
return a value bigger than RAND_MAX... */
|
||||||
/* return a value bigger than RAND_MAX... */
|
double r = (double)(rand()%RAND_MAX) / ((double)RAND_MAX+1.0);
|
||||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
int l = luaL_opt_int(1, 0);
|
||||||
double l = luaL_opt_number(1, 0);
|
|
||||||
if (l == 0)
|
if (l == 0)
|
||||||
lua_pushnumber(r);
|
lua_pushnumber(r);
|
||||||
else
|
else {
|
||||||
lua_pushnumber((int)(r*l)+1);
|
int u = luaL_opt_int(2, 0);
|
||||||
|
if (u == 0) {
|
||||||
|
u = l;
|
||||||
|
l = 1;
|
||||||
|
}
|
||||||
|
luaL_arg_check(l<=u, 1, "interval is empty");
|
||||||
|
lua_pushnumber((int)(r*(u-l+1))+l);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue