mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
"random" accepts an 'n' to return between 1 and 'n'.
This commit is contained in:
parent
0892f0e5b7
commit
caa987faad
10
manual.tex
10
manual.tex
@ -1,4 +1,4 @@
|
|||||||
% $Id: manual.tex,v 2.10 1997/07/02 17:09:48 roberto Exp roberto $
|
% $Id: manual.tex,v 2.11 1997/07/04 22:35:38 roberto Exp roberto $
|
||||||
|
|
||||||
\documentstyle[fullpage,11pt,bnf]{article}
|
\documentstyle[fullpage,11pt,bnf]{article}
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ Waldemar Celes
|
|||||||
\tecgraf\ --- Computer Science Department --- PUC-Rio
|
\tecgraf\ --- Computer Science Department --- PUC-Rio
|
||||||
}
|
}
|
||||||
|
|
||||||
\date{\small \verb$Date: 1997/07/02 17:09:48 $}
|
\date{\small \verb$Date: 1997/07/04 22:35:38 $}
|
||||||
|
|
||||||
\maketitle
|
\maketitle
|
||||||
|
|
||||||
@ -2085,8 +2085,10 @@ Both can be used with an unlimited number of arguments.
|
|||||||
The functions \verb|random| and \verb|randomseed| are interfaces to
|
The functions \verb|random| and \verb|randomseed| are interfaces to
|
||||||
the simple random generator functions \verb|rand| and \verb|srand|,
|
the simple random generator functions \verb|rand| and \verb|srand|,
|
||||||
provided by ANSI C.
|
provided by ANSI C.
|
||||||
The function \verb|random| returns pseudo-random numbers in the
|
The function \verb|random|, when called without arguments,
|
||||||
range \Math{[0,1)}.
|
returns a pseudo-random real number in the range \Math{[0,1)}.
|
||||||
|
When called with a number \Math{n},
|
||||||
|
returns a pseudo-random integer in the range \Math{[1,n]}.
|
||||||
|
|
||||||
|
|
||||||
\subsection{I/O Facilities} \label{libio}
|
\subsection{I/O Facilities} \label{libio}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
** Mathematics library to LUA
|
** Mathematics library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_mathlib="$Id: mathlib.c,v 1.24 1997/06/09 17:30:10 roberto Exp roberto $";
|
char *rcs_mathlib="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@ -171,7 +171,11 @@ static void math_rad (void)
|
|||||||
|
|
||||||
static void math_random (void)
|
static void math_random (void)
|
||||||
{
|
{
|
||||||
lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
|
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||||
|
if (lua_getparam(1) == LUA_NOOBJECT)
|
||||||
|
lua_pushnumber(r);
|
||||||
|
else
|
||||||
|
lua_pushnumber((int)(r*luaL_check_number(1)) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void math_randomseed (void)
|
static void math_randomseed (void)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user