diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-07-30 19:15:18 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-07-30 19:15:18 -0300 |
| commit | caa987faad6191a2abaf9d2741216ba619359fdb (patch) | |
| tree | ce14080b8ed31afc2f9207d10c41f8df40b70816 /mathlib.c | |
| parent | 0892f0e5b75c51f1fee07276a3ba13301b83409e (diff) | |
| download | lua-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.c | 8 |
1 files changed, 6 insertions, 2 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.24 1997/06/09 17:30:10 roberto Exp roberto $"; | 6 | char *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 | ||
| 172 | static void math_random (void) | 172 | static 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 | ||
| 177 | static void math_randomseed (void) | 181 | static void math_randomseed (void) |
