1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" References consulted:
.\" Linux libc source code
.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
.\" 386BSD man pages
.\" Modified Mon Mar 29 22:48:44 1993, David Metcalfe
.\" Modified Wed Apr 28 01:35:00 1993, Lars Wirzenius
.\" Modified Sat Jul 24 18:39:41 1993, Rik Faith (faith@cs.unc.edu)
.\" Modified Thu May 18 10:10:13 1995, Rik Faith (faith@cs.unc.edu) to add
.\" better discussion of problems with rand on other systems.
.\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
.\" "
.\" Translated into Spanish Thu Mar 5 15:51:13 CET 1998 by Gerardo
.\" Aburruzaga Garca <gerardo.aburruzaga@uca.es>
.\"
.TH RAND 3 "5 Marzo 1998" "GNU" "Manual del Programador de Linux"
.SH NOMBRE
rand, srand \- generador de nmeros pseudo-aleatorios
.SH SINOPSIS
.nf
.B #include <stdlib.h>
.sp
.B int rand(void);
.sp
.BI "void srand(unsigned int " semilla );
.fi
.SH DESCRIPCIN
La funcin \fBrand()\fP devuelve un entero pseudo-aleatorio entre 0 y
\fBRAND_MAX\fR.
.PP
La funcin \fBsrand()\fP establece su argumento como la \fIsemilla\fP
de una nueva serie de enteros seudo-aleatorios que sern devueltos en
secuencia por \fBrand()\fP.
Estas secuencias son repetibles si se llama a \fBsrand()\fP con el
mismo valor para
.IR semilla .
.PP
Si no se proporciona ningn valor para la semilla (i.e., no se llama a
.BR srand() ),
la funcin \fBrand()\fP automticamente coge el valor 1 para la semilla.
.SH "VALOR DEVUELTO"
La funcin \fBrand()\fP devuelve un valor entre 0 y RAND_MAX.
La funcin \fBsrand()\fP no devuelve nada.
.SH OBSERVACIONES
Las versiones de \fBrand()\fP y \fBsrand()\fP de la Biblioteca de C de
Linux emplean el mismo generador de nmeros aleatorios que
\fBrandom()\fP y \fBsrandom()\fP, de modo que los bits de orden ms
bajo deberan ser tan aleatorios como los de orden ms alto.
Sin embargo, en implementaciones de
.B rand()
ms antiguas, los bits de ms bajo orden son mucho menos aleatorios
que los de orden ms alto.
.PP
En
.I Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
T. Vetterling; New York: Cambridge University Press, 1990 (1 ed,
pg. 207)), se hacen los siguientes comentarios:
.RS
"Si Ud. quiere generar un entero aleatorio entre 1 y 10, siempre
debera hacerlo as:
.RS
.sp
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
.sp
.RE
y nunca con algo parecido a esto:
.RS
.sp
j=1+((int) (1000000.0*rand()) % 10);
.sp
.RE
(que utiliza bits de ms bajo orden)."
.RE
.PP
La generacin de nmeros aleatorios es un tema complejo. El libro
.I Numerical Recipes in C
(ver la referencia anterior)
proporciona una excelente discusin sobre la generacin prctica de
nmeros aleatorios en el captulo 7 (Nmeros Aleatorios).
.PP
Para una discusin ms terica que tambin cubra en profundidad otros
detalles prcticos, por favor consulte el captulo 3 (Nmeros
Aleatorios) en el libro de Donald E. Knuth
.IR "The Art of Computer Programming" ,
volumen 2 (Seminumerical Algorithms), 2 ed.; Reading, Massachusetts:
Addison-Wesley Publishing Company, 1981.
.SH "CONFORME A"
SVID 3, BSD 4.3, ISO 9899
.SH "VASE TAMBIN"
.BR random "(3), " srandom "(3), " initstate "(3), " setstate (3)
|