aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-02-09 15:21:27 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-02-09 15:21:27 -0200
commit3abc25fa5424ebc857d445792dd0689926b7ea34 (patch)
tree4c8c00598e952bf7340f24d1f5bb347f7636cd34
parentf4d67761f1ee3de81ae720703011f5c03f1bfb47 (diff)
downloadlua-3abc25fa5424ebc857d445792dd0689926b7ea34.tar.gz
lua-3abc25fa5424ebc857d445792dd0689926b7ea34.tar.bz2
lua-3abc25fa5424ebc857d445792dd0689926b7ea34.zip
new functions "random" and "randomseed".
-rw-r--r--manual.tex15
-rw-r--r--mathlib.c19
2 files changed, 28 insertions, 6 deletions
diff --git a/manual.tex b/manual.tex
index e210a039..b3e37013 100644
--- a/manual.tex
+++ b/manual.tex
@@ -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
32Departamento de Inform\'atica --- PUC-Rio 32Departamento 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}
1297abs acos asin atan atan2 ceil cos floor 1298abs acos asin atan atan2 ceil cos floor log log10
1298log log10 max min mod sin sqrt tan 1299max min mod sin sqrt tan random randomseed
1299\end{verbatim} 1300\end{verbatim}
1300Most of them 1301Most of them
1301are only interfaces to the homonymous functions in the C library, 1302are 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
1310The function \verb'mod' is equivalent to the \verb'%' operator in C. 1311The function \verb'mod' is equivalent to the \verb'%' operator in C.
1311 1312
1313The functions \verb'random' and \verb'randomseed' are interfaces to
1314the simple random generator functions \verb'rand' and \verb'srand',
1315provided by ANSI C.
1316The 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
diff --git a/mathlib.c b/mathlib.c
index 1092052a..cf5b6a8a 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,9 +3,9 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $"; 6char *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
187static void math_random (void)
188{
189 lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
190}
191
192static 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}