.\" 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).) .\" Modified Fri Apr 10 01:47:03 1998, Nicolás Lichtmaier .\" with contribution from Francesco Potorti .\" " .\" Translated into Spanish Thu Mar 5 15:51:13 CET 1998 by Gerardo .\" Aburruzaga García .\" .TH RAND 3 "18 mayo 1995" "GNU" "Manual del Programador de Linux" .SH NOMBRE rand, srand \- generador de números pseudo-aleatorios .SH SINOPSIS .nf .B #include .sp .B int rand(void); .sp .BI "void srand(unsigned int " semilla ); .fi .SH DESCRIPCIÓN La función \fBrand()\fP devuelve un entero pseudo-aleatorio entre 0 y \fBRAND_MAX\fR. .PP La función \fBsrand()\fP establece su argumento como la \fIsemilla\fP de una nueva serie de enteros seudo-aleatorios que serán 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 ningún valor para la semilla (i.e., no se llama a .BR srand() ), la función \fBrand()\fP automáticamente coge el valor 1 para la semilla. .SH "VALOR DEVUELTO" La función \fBrand()\fP devuelve un valor entre 0 y RAND_MAX. La función \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 números aleatorios que \fBrandom()\fP y \fBsrandom()\fP, de modo que los bits de orden más bajo deberían ser tan aleatorios como los de orden más alto. Sin embargo, en implementaciones de .B rand() más antiguas, los bits de más bajo orden son mucho menos aleatorios que los de orden más 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 (2ª ed, pág. 277)), se hacen los siguientes comentarios: .RS "Si Ud. quiere generar un entero aleatorio entre 1 y 10, siempre debería hacerlo usando los bits de orden alto como en: .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+(rand() % 10); .sp .RE (que utiliza bits de más bajo orden)." .RE .PP La generación de números aleatorios es un tema complejo. El libro .I Numerical Recipes in C (ver la referencia anterior) proporciona una excelente discusión sobre la generación práctica de números aleatorios en el capítulo 7 (Números Aleatorios). .PP Para una discusión más teórica que también cubra en profundidad otros detalles prácticos, por favor consulte el capítulo 3 (Números 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 "VÉASE TAMBIÉN" .BR random "(3), " srandom "(3), " initstate "(3), " setstate (3)