aboutsummaryrefslogtreecommitdiff
path: root/mathlib.c
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 /mathlib.c
parentf4d67761f1ee3de81ae720703011f5c03f1bfb47 (diff)
downloadlua-3abc25fa5424ebc857d445792dd0689926b7ea34.tar.gz
lua-3abc25fa5424ebc857d445792dd0689926b7ea34.tar.bz2
lua-3abc25fa5424ebc857d445792dd0689926b7ea34.zip
new functions "random" and "randomseed".
Diffstat (limited to 'mathlib.c')
-rw-r--r--mathlib.c19
1 files changed, 17 insertions, 2 deletions
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}