Thread View: de.comp.lang.perl
3 messages
3 total messages
Started by Jörg "Yadgar" Bleimann
Sat, 24 Oct 2026 03:14
Anfängerfrage: wieso liefert rand() auch ohne srand() bei jedem Durchlauf unterschiedliche Werte?
Author: Jörg "Yadgar" Bleimann
Date: Sat, 24 Oct 2026 03:14
Date: Sat, 24 Oct 2026 03:14
15 lines
1736 bytes
1736 bytes
Hi(gh)! 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? Bis bald im Khyberspace! Yadgar
Re: Anfängerfrage: wieso liefert rand() auch ohne srand() bei jedem Durchlauf unterschiedliche Werte?
Author: 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
Re: Anfängerfrage: wieso liefert rand() auch ohne srand() bei jedem Durchlauf unterschiedliche Werte?
Author: Stefan Ram
Date: Sat, 24 Oct 2026 09:37
Date: Sat, 24 Oct 2026 09:37
10 lines
1850 bytes
1850 bytes
=?UTF-8?Q?J=C3=B6rg_=22Yadgar=22_Bleimann?= <yazdegird@gmx.de> schrieb oder zitierte: > Wird ohne srand() bei Verwendung von rand() >srand() implizit verwendet? $ perldoc -f rand . . . Automatically calls "srand" unless "srand" has already been called. . . .
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads