.\" 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 to German Sat May 18 19:00:00 1996 by Patrick Rother .\" .TH RAND 3 "18. Mai 1996" "GNU" "Bibliotheksfunktionen" .SH BEZEICHNUNG rand, srand \- Zufallszahlengenerator .SH ÜBERSICHT .nf .B #include .sp .B int rand(void); .sp .BI "void srand(unsigned int " seed ); .fi .SH BESCHREIBUNG Die Funktion .B rand() liefert eine Pseudozufalls-Ganzzahl (integer) zwischen 0 und .BR RAND_MAX . .PP Die Funktion .B srand() setzt ihr Argument als Ursprung für eine neue Reihe von Pseudozufalls-Ganzzahlen ein, welche von .B rand() geliefert werden. Diese Sequenzen sind durch Aufruf von .B srand() mit dem selben Ursprungswert wiederholbar. .PP Wenn kein Ursprungswert angegeben wird, wird 1 als Ursprungswert für .B rand() angenommen. .SH "RÜCKGABEWERT" Die Funktion .B rand() liefert einen Wert zwischen 0 und .BR RAND_MAX . Die Funktion .B srand() liefert keinen Wert zurück. .SH ANMERKUNGEN Die Versionen von .BR rand() " und " srand() in der Linux C-Bibliothek benutzen den selben Zufallszahlengenerator wie .BR random() " und " srandom() , d.h. niederwertige Bits sind genauso zufällig wie höherwertige Bits. Bei älteren Implementationen von .B rand() sind niederwertige Bits jedoch viel weniger zufällig als höherwertige Bits. .PP In .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 (1st ed, p. 207)), finden sich die folgenden Kommentare: .RS "Wenn Sie Zufalls-Ganzzahlen zwischen 1 und 10 erzeugen möchten, sollten Sie dies immer wie folgt tun: .RS .sp j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); .sp .RE und niemals auf folgene oder ähnliche Weise: .RS .sp j=1+((int) (1000000.0*rand()) % 10); .sp .RE (wodurch niederwertige Bits benutzt würden)." .RE .PP Zufallszahlenerzeugung ist ein kompliziertes Thema. Das Buch .I Numerical Recipes in C (siehe oben) liefert eine exzellente Diskussion über praktische Zufallszahlenerzeugung in Kapitel 7 (Zufallszahlen). .PP Für eine mehr theoretische Diskussion, die auch viele praktische Aspekte behandelt, sehen Sie bitte Kapitel 3 (Zufallszahlen) in Donald E. Knuths .IR "The Art of Computer Programming" , volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981. .SH "KONFORM ZU" SVID 3, BSD 4.3, ISO 9899 .SH "SIEHE AUCH" .BR random (3), .BR srandom (3), .BR initstate (3), .BR setstate (3).