diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 15:21:27 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 15:21:27 -0200 |
commit | 3abc25fa5424ebc857d445792dd0689926b7ea34 (patch) | |
tree | 4c8c00598e952bf7340f24d1f5bb347f7636cd34 | |
parent | f4d67761f1ee3de81ae720703011f5c03f1bfb47 (diff) | |
download | lua-3abc25fa5424ebc857d445792dd0689926b7ea34.tar.gz lua-3abc25fa5424ebc857d445792dd0689926b7ea34.tar.bz2 lua-3abc25fa5424ebc857d445792dd0689926b7ea34.zip |
new functions "random" and "randomseed".
-rw-r--r-- | manual.tex | 15 | ||||
-rw-r--r-- | mathlib.c | 19 |
2 files changed, 28 insertions, 6 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.6 1996/02/05 21:32:19 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.7 1996/02/09 16:37:58 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentstyle[A4,11pt,bnf]{article} | 3 | \documentstyle[A4,11pt,bnf]{article} |
4 | 4 | ||
@@ -32,7 +32,7 @@ Waldemar Celes Filho | |||
32 | Departamento de Inform\'atica --- PUC-Rio | 32 | Departamento de Inform\'atica --- PUC-Rio |
33 | } | 33 | } |
34 | 34 | ||
35 | \date{\small \verb$Date: 1996/02/05 21:32:19 $} | 35 | \date{\small \verb$Date: 1996/02/09 16:37:58 $} |
36 | 36 | ||
37 | \maketitle | 37 | \maketitle |
38 | 38 | ||
@@ -1293,9 +1293,10 @@ The library provides the following functions: | |||
1293 | \Deffunc{atan2}\Deffunc{ceil}\Deffunc{cos}\Deffunc{floor} | 1293 | \Deffunc{atan2}\Deffunc{ceil}\Deffunc{cos}\Deffunc{floor} |
1294 | \Deffunc{log}\Deffunc{log10}\Deffunc{max}\Deffunc{min} | 1294 | \Deffunc{log}\Deffunc{log10}\Deffunc{max}\Deffunc{min} |
1295 | \Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} | 1295 | \Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} |
1296 | \Deffunc{random}\Deffunc{randomseed} | ||
1296 | \begin{verbatim} | 1297 | \begin{verbatim} |
1297 | abs acos asin atan atan2 ceil cos floor | 1298 | abs acos asin atan atan2 ceil cos floor log log10 |
1298 | log log10 max min mod sin sqrt tan | 1299 | max min mod sin sqrt tan random randomseed |
1299 | \end{verbatim} | 1300 | \end{verbatim} |
1300 | Most of them | 1301 | Most of them |
1301 | are only interfaces to the homonymous functions in the C library, | 1302 | are only interfaces to the homonymous functions in the C library, |
@@ -1309,6 +1310,12 @@ Both can be used with an unlimited number of arguments. | |||
1309 | 1310 | ||
1310 | The function \verb'mod' is equivalent to the \verb'%' operator in C. | 1311 | The function \verb'mod' is equivalent to the \verb'%' operator in C. |
1311 | 1312 | ||
1313 | The functions \verb'random' and \verb'randomseed' are interfaces to | ||
1314 | the simple random generator functions \verb'rand' and \verb'srand', | ||
1315 | provided by ANSI C. | ||
1316 | The function \verb'random' returns pseudo-random numbers in the range | ||
1317 | $[0,1)$. | ||
1318 | |||
1312 | 1319 | ||
1313 | \subsection{I/O Facilities} \label{libio} | 1320 | \subsection{I/O Facilities} \label{libio} |
1314 | 1321 | ||
@@ -3,9 +3,9 @@ | |||
3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> /* NULL */ | 8 | #include <stdlib.h> |
9 | #include <math.h> | 9 | #include <math.h> |
10 | 10 | ||
11 | #include "lualib.h" | 11 | #include "lualib.h" |
@@ -184,6 +184,18 @@ static void math_rad (void) | |||
184 | lua_pushnumber (d/180.*PI); | 184 | lua_pushnumber (d/180.*PI); |
185 | } | 185 | } |
186 | 186 | ||
187 | static void math_random (void) | ||
188 | { | ||
189 | lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX); | ||
190 | } | ||
191 | |||
192 | static void math_randomseed (void) | ||
193 | { | ||
194 | srand(lua_check_number(1, "randomseed")); | ||
195 | } | ||
196 | |||
197 | |||
198 | |||
187 | /* | 199 | /* |
188 | ** Open math library | 200 | ** Open math library |
189 | */ | 201 | */ |
@@ -208,5 +220,8 @@ void mathlib_open (void) | |||
208 | lua_register ("exp", math_exp); | 220 | lua_register ("exp", math_exp); |
209 | lua_register ("deg", math_deg); | 221 | lua_register ("deg", math_deg); |
210 | lua_register ("rad", math_rad); | 222 | lua_register ("rad", math_rad); |
223 | lua_register ("random", math_random); | ||
224 | lua_register ("randomseed", math_randomseed); | ||
225 | |||
211 | old_pow = lua_lockobject(lua_setfallback("arith", math_pow)); | 226 | old_pow = lua_lockobject(lua_setfallback("arith", math_pow)); |
212 | } | 227 | } |