PHP - random_int
Trieda
Metóda - random_int
(PHP 7)
random_int() generates a pseudo-random integer using a
cryptographically secure method.
The interval in which the generated numbers will be can be set using the
$max and $min parameters, and is inclusive.
The function uses the same entropy sources as
random_bytes().
Procedurálne
- function random_int () : int
Parametre
ŽiadneMávratovej hodnoty
Vracia: int
A cryptographically secure random integer.
Príklady
Choosing the winner from a list of participants:
<?php
$participants = ["John Brown", "James Smith", "Thomas Raspberry"];
$winner = random_int(0, count($participants) - 1);
echo "Winner of Ericsson T20i is: " . $participants[$winner];
?>
Generating a lottery draw:
<?php
$winningNumbers = [];
for ($i = 0; $i < 5; $i++) {
$number = 0;
while ($number == 0 || in_array($number, $winningNumbers)) {
$number = random_int(1, 69);
}
array_push($winningNumbers, $number);
}
echo "The powerball winning numbers are: " . join(" ", $winningNumbers)
. ". Powerball: " random_int(1, 26);
?>
Súvisiace manuály
- function random_bytes (int $length) : string
