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 113 114 115 116 117 118 119 120 121 122 123
|
.\" 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 <krd@gulu.net>
.\"
.TH RAND 3 "18. Mai 1996" "GNU" "Bibliotheksfunktionen"
.SH BEZEICHNUNG
rand, srand \- Zufallszahlengenerator
.SH EBERSICHT
.nf
.B #include <stdlib.h>
.sp
.B int rand(void);
.sp
.BI "void srand(unsigned int " seed );
.fi
.SH BESCHREIBUNG
Die Funktion
.B rand()
liefert eine Pseudozufalls-Ganzzahl (integer) zwischem 0 und
.BR RAND_MAX .
.PP
Die Funktion
.B srand()
setzt ihr Argument als Ursprung fr 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 fr
.B rand()
angenommen.
.SH "RCKGABEWERT"
Die Funktion
.B rand()
liefert einen Wert zwischen 0 und
.BR RAND_MAX .
Die Funktion
.B srand()
liefert keinen Wert zurck.
.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. niederwerte Bits sind genauso zufllig wie hherwertige Bits.
Bei lteren Implementationen von
.B rand()
sind niederwerte Bits jedoch viel weniger zufllig als hherwertige 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 mchten, 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 niederwerte Bits benutzt wrden)."
.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
Fr 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).
|