diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-04-10 14:53:33 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-04-10 14:53:33 -0300 |
commit | 5336cc9d6a431fb32b511ae4980cbd68789cec02 (patch) | |
tree | 53a06610596f7e1a7c9a4551ab44565b29f5d68d /lmathlib.c | |
parent | fdd137276b73eec65545f8240e155df2072c8882 (diff) | |
download | lua-5336cc9d6a431fb32b511ae4980cbd68789cec02.tar.gz lua-5336cc9d6a431fb32b511ae4980cbd68789cec02.tar.bz2 lua-5336cc9d6a431fb32b511ae4980cbd68789cec02.zip |
math.random: use 'random' when available + changes to work correctly
for any integer interval (up to [minint,maxint])
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 27 |
1 files changed, 19 insertions, 8 deletions
@@ -21,6 +21,17 @@ | |||
21 | #define PI (l_mathop(3.141592653589793238462643383279502884)) | 21 | #define PI (l_mathop(3.141592653589793238462643383279502884)) |
22 | 22 | ||
23 | 23 | ||
24 | #if !defined(l_rand) /* { */ | ||
25 | #if defined(LUA_USE_POSIX) | ||
26 | #define l_rand() random() | ||
27 | #define l_srand(x) srandom(x) | ||
28 | #else | ||
29 | #define l_rand() rand() | ||
30 | #define l_srand(x) srand(x) | ||
31 | #endif | ||
32 | #endif /* } */ | ||
33 | |||
34 | |||
24 | static int math_abs (lua_State *L) { | 35 | static int math_abs (lua_State *L) { |
25 | if (lua_isinteger(L, 1)) { | 36 | if (lua_isinteger(L, 1)) { |
26 | lua_Integer n = lua_tointeger(L, 1); | 37 | lua_Integer n = lua_tointeger(L, 1); |
@@ -146,7 +157,7 @@ static int math_log (lua_State *L) { | |||
146 | res = l_mathop(log)(x); | 157 | res = l_mathop(log)(x); |
147 | else { | 158 | else { |
148 | lua_Number base = luaL_checknumber(L, 2); | 159 | lua_Number base = luaL_checknumber(L, 2); |
149 | if (base == (lua_Number)10.0) res = l_mathop(log10)(x); | 160 | if (base == 10.0) res = l_mathop(log10)(x); |
150 | else res = l_mathop(log)(x)/l_mathop(log)(base); | 161 | else res = l_mathop(log)(x)/l_mathop(log)(base); |
151 | } | 162 | } |
152 | lua_pushnumber(L, res); | 163 | lua_pushnumber(L, res); |
@@ -219,10 +230,10 @@ static int math_max (lua_State *L) { | |||
219 | 230 | ||
220 | 231 | ||
221 | static int math_random (lua_State *L) { | 232 | static int math_random (lua_State *L) { |
222 | /* the `%' avoids the (rare) case of r==1, and is needed also because on | ||
223 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ | ||
224 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; | ||
225 | lua_Integer low, up; | 233 | lua_Integer low, up; |
234 | lua_Number r = (lua_Number)l_rand() * (1.0 / ((lua_Number)RAND_MAX + 1.0)); | ||
235 | if (r >= 1.0) /* can occur if lua_Number has no precision for RAND_MAX */ | ||
236 | r = 0.0; /* statistically irrelevant; avoids out-of-range results */ | ||
226 | switch (lua_gettop(L)) { /* check number of arguments */ | 237 | switch (lua_gettop(L)) { /* check number of arguments */ |
227 | case 0: { /* no arguments */ | 238 | case 0: { /* no arguments */ |
228 | lua_pushnumber(L, r); /* Number between 0 and 1 */ | 239 | lua_pushnumber(L, r); /* Number between 0 and 1 */ |
@@ -241,15 +252,15 @@ static int math_random (lua_State *L) { | |||
241 | default: return luaL_error(L, "wrong number of arguments"); | 252 | default: return luaL_error(L, "wrong number of arguments"); |
242 | } | 253 | } |
243 | /* random integer in the interval [low, up] */ | 254 | /* random integer in the interval [low, up] */ |
244 | up++; /* change interval to [low, up) */ | 255 | luaL_argcheck(L, low <= up, 1, "interval is empty"); |
245 | luaL_argcheck(L, up - low > 0, 1, "interval is empty"); | 256 | r *= ((lua_Number)((lua_Unsigned)up - (lua_Unsigned)low) + 1.0); |
246 | lua_pushinteger(L, (lua_Integer)(r * (lua_Number)(up - low)) + low); | 257 | lua_pushinteger(L, (lua_Integer)((lua_Unsigned)r + (lua_Unsigned)low)); |
247 | return 1; | 258 | return 1; |
248 | } | 259 | } |
249 | 260 | ||
250 | 261 | ||
251 | static int math_randomseed (lua_State *L) { | 262 | static int math_randomseed (lua_State *L) { |
252 | srand((unsigned int)luaL_checkunsigned(L, 1)); | 263 | l_srand((unsigned int)luaL_checkunsigned(L, 1)); |
253 | (void)rand(); /* discard first value to avoid undesirable correlations */ | 264 | (void)rand(); /* discard first value to avoid undesirable correlations */ |
254 | return 0; | 265 | return 0; |
255 | } | 266 | } |