aboutsummaryrefslogtreecommitdiff
path: root/mathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-07-30 19:15:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-07-30 19:15:18 -0300
commitcaa987faad6191a2abaf9d2741216ba619359fdb (patch)
treece14080b8ed31afc2f9207d10c41f8df40b70816 /mathlib.c
parent0892f0e5b75c51f1fee07276a3ba13301b83409e (diff)
downloadlua-caa987faad6191a2abaf9d2741216ba619359fdb.tar.gz
lua-caa987faad6191a2abaf9d2741216ba619359fdb.tar.bz2
lua-caa987faad6191a2abaf9d2741216ba619359fdb.zip
"random" accepts an 'n' to return between 1 and 'n'.
Diffstat (limited to 'mathlib.c')
-rw-r--r--mathlib.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/mathlib.c b/mathlib.c
index bb33db08..3be26b67 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,7 +3,7 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.24 1997/06/09 17:30:10 roberto Exp roberto $"; 6char *rcs_mathlib="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <math.h> 9#include <math.h>
@@ -171,7 +171,11 @@ static void math_rad (void)
171 171
172static void math_random (void) 172static void math_random (void)
173{ 173{
174 lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX); 174 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
175 if (lua_getparam(1) == LUA_NOOBJECT)
176 lua_pushnumber(r);
177 else
178 lua_pushnumber((int)(r*luaL_check_number(1)) + 1);
175} 179}
176 180
177static void math_randomseed (void) 181static void math_randomseed (void)