Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Perl - generieren einer random-zahl

Empfohlene Antworten

Veröffentlicht

Moin Leute,

und wieder steh ich vor einem ungelöstem Problem ;-)

Wie generiert man unter Perl eine random-zahl? Unter C gabs früher (ganz ganz früer ;-) mal so ne funktion. Gibts da auch was für Perl.

Gruß

euer

Raudi

Hallo Raudi!

folgendes steht zu deinem Problem im Perl-Cookbook.

Ich hoffe du kannst Englisch.

---------------------------------------------------

2.7. Generating Random Numbers

Problem

You want to make random numbers in a given range, inclusive, such as when you randomly pick an array index, simulate rolling a die in a game of chance, or generate a random password.

Solution

Use Perl's rand function.

$random = int( rand( $Y-$X+1 ) ) + $X;

Discussion

This code generates and prints a random integer between 25 and 75, inclusive:

$random = int( rand(51)) + 25;

print "$random\n";

The rand function returns a fractional number, from (and including) 0 up to (but not including) its argument. We give it an argument of 51 to get a number that can be 0 or more, but never 51 or more. We take the integer portion of this to get a number from 0 to 50, inclusive (50.99999.... will be turned to 50 by int). We then add 25 to it, to get a number from 25 to 75, inclusive.

A common application of this is the random selection of an element from an array:

$elt = $array[ rand @array ];

And generating a random password from a sequence of characters:

@chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) );

$password = join("", @chars[ map { rand @chars } ( 1 .. 8 ) ]);

We use map to generate eight random indices into @chars, extract the corresponding characters with a slice, and join them together to form the random password. This isn't a good random number, though, as its security relies on the choice of seed, which is based on the time the program started. See Recipe 2.8 for a way to better seed your random number generator.

--------------------------------------------------

Gruß Jaraz

Super - hat geklappt

danke ;-)

Raudi

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.