lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 3abc25fa5424ebc857d445792dd0689926b7ea34
parent f4d67761f1ee3de81ae720703011f5c03f1bfb47
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Fri,  9 Feb 1996 15:21:07 -0200

new functions "random" and "randomseed".

Diffstat:
Mmanual.tex | 15+++++++++++----
Mmathlib.c | 19+++++++++++++++++--
2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/manual.tex b/manual.tex @@ -1,4 +1,4 @@ -% $Id: manual.tex,v 1.6 1996/02/05 21:32:19 roberto Exp roberto $ +% $Id: manual.tex,v 1.7 1996/02/09 16:37:58 roberto Exp roberto $ \documentstyle[A4,11pt,bnf]{article} @@ -32,7 +32,7 @@ Waldemar Celes Filho Departamento de Inform\'atica --- PUC-Rio } -\date{\small \verb$Date: 1996/02/05 21:32:19 $} +\date{\small \verb$Date: 1996/02/09 16:37:58 $} \maketitle @@ -1293,9 +1293,10 @@ The library provides the following functions: \Deffunc{atan2}\Deffunc{ceil}\Deffunc{cos}\Deffunc{floor} \Deffunc{log}\Deffunc{log10}\Deffunc{max}\Deffunc{min} \Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} +\Deffunc{random}\Deffunc{randomseed} \begin{verbatim} -abs acos asin atan atan2 ceil cos floor -log log10 max min mod sin sqrt tan +abs acos asin atan atan2 ceil cos floor log log10 +max min mod sin sqrt tan random randomseed \end{verbatim} Most of them 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. The function \verb'mod' is equivalent to the \verb'%' operator in C. +The functions \verb'random' and \verb'randomseed' are interfaces to +the simple random generator functions \verb'rand' and \verb'srand', +provided by ANSI C. +The function \verb'random' returns pseudo-random numbers in the range +$[0,1)$. + \subsection{I/O Facilities} \label{libio} diff --git a/mathlib.c b/mathlib.c @@ -3,9 +3,9 @@ ** Mathematics library to LUA */ -char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $"; +char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp roberto $"; -#include <stdio.h> /* NULL */ +#include <stdlib.h> #include <math.h> #include "lualib.h" @@ -184,6 +184,18 @@ static void math_rad (void) lua_pushnumber (d/180.*PI); } +static void math_random (void) +{ + lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX); +} + +static void math_randomseed (void) +{ + srand(lua_check_number(1, "randomseed")); +} + + + /* ** Open math library */ @@ -208,5 +220,8 @@ void mathlib_open (void) lua_register ("exp", math_exp); lua_register ("deg", math_deg); lua_register ("rad", math_rad); + lua_register ("random", math_random); + lua_register ("randomseed", math_randomseed); + old_pow = lua_lockobject(lua_setfallback("arith", math_pow)); }