Article View: de.comp.lang.perl
Article #2Re: Anfängerfrage: wieso liefert rand() auch ohne srand() bei jedem Durchlauf unterschiedliche Werte?
From: Peter J. Holzer
Date: Sat, 24 Oct 2026 08:26
Date: Sat, 24 Oct 2026 08:26
28 lines
2207 bytes
2207 bytes
On 2026-10-24 05:14, Jörg Yadgar Bleimann <yazdegird@gmx.de> wrote:
> Habe erst kürzlich angefangen, das Perl-Tutorial von Eike Grote (Version
> 2.05) durchzuarbeiten und bin jetzt gerade bei den Zufallszahlen (S.
> 29), wo beschrieben wird, wie rand() und srand() zusammenwirken... ich
> habe das Beispielprogramm abgetippt und gestartet - und wundere mich,
> dass nicht nur die dritte Variante der Ausgabeschleife (srand() ohne
> Parameter), sondern auch die erste bei jedem Programmlauf neue Werte
> erzeugt (natürlich aber nicht die zweite, wo ein fester Parameter für
> srand() verwendet wird). Wird ohne srand() bei Verwendung von rand()
> srand() implizit verwendet?
Ja. Aus »perldoc -f rand«:
| rand EXPR
| rand Returns a random fractional number greater than or equal to 0
| and less than the value of EXPR. (EXPR should be positive.) If
| EXPR is omitted, the value 1 is used.
| [...]
| Automatically calls "srand" unless "srand" has already been
| called. See also "srand".
und »perldoc -f srand«:
| If "srand" is not called explicitly, it is called implicitly
| without a parameter at the first use of the "rand" operator.
hjp
Technical Details
Message-ID:
<slrn11doqtg.3oo8l.hjp-usenet4@trintignant.hjp.at>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.xs3.de!tahina.priv.at!news.luga.at!.POSTED.10.197.38.6!not-for-mailReferences:
<11bh7r5$859k$1@solani.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.xs3.de!tahina.priv.at!news.luga.at!.POSTED.10.197.38.6!not-for-mail From: "Peter J. Holzer" <hjp-usenet4@hjp.at> Newsgroups: de.comp.lang.perl Subject: Re: Anfängerfrage: wieso liefert rand() auch ohne srand() bei jedem Durchlauf unterschiedliche Werte? Date: Sat, 24 Oct 2026 10:26:24 +0200 Organization: LUGA Message-ID: <slrn11doqtg.3oo8l.hjp-usenet4@trintignant.hjp.at> References: <11bh7r5$859k$1@solani.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: vinge.luga.at; posting-host="10.197.38.6"; logging-data="283864"; mail-complaints-to="usenet@vinge.luga.at" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:n7LIDEDmilC0aqfmnwZRsAzyDrQ=