Tools for generating random iteration sequences.
Generate random selections from an array of values.
Random::choice(array $items, int $repetitions)
use IterTools\Random;
$cards = ['Ace', 'King', 'Queen', 'Jack', 'Joker'];
$repetitions = 10;
foreach (Random::choice($cards, $repetitions) as $card) {
print($card);
}
// 'King', 'Jack', 'King', 'Ace', ... [random]Generate random coin flips (0 or 1).
Random::coinFlip(int $repetitions)
use IterTools\Random;
$repetitions = 10;
foreach (Random::coinFlip($repetitions) as $coinFlip) {
print($coinFlip);
}
// 1, 0, 1, 1, 0, ... [random]Generate random numbers (integers).
Random::number(int $min, int $max, int $repetitions)
use IterTools\Random;
$min = 1;
$max = 4;
$repetitions = 10;
foreach (Random::number($min, $max, $repetitions) as $number) {
print($number);
}
// 3, 2, 5, 5, 1, 2, ... [random]Generate a random percentage between 0 and 1.
Random::percentage(int $repetitions)
use IterTools\Random;
$repetitions = 10;
foreach (Random::percentage($repetitions) as $percentage) {
print($percentage);
}
// 0.30205562629132, 0.59648594775233, ... [random]Generate random rock-paper-scissors hands.
Random::rockPaperScissors(int $repetitions)
use IterTools\Random;
$repetitions = 10;
foreach (Random::rockPaperScissors($repetitions) as $rpsHand) {
print($rpsHand);
}
// 'paper', 'rock', 'rock', 'scissors', ... [random]Sample $size elements from the population without replacement.
Random::sample(iterable $data, int $size, ?\Random\Engine $engine = null)
- Every input position is used at most once; duplicate values in the population are valid.
- Materializes the input. Output keys are sequential 0-indexed.
- Throws
\InvalidArgumentExceptionif$sizeis negative. - Throws
\LengthExceptionif$sizeexceeds the population size.
use IterTools\Random;
$population = ['a', 'b', 'c', 'd', 'e'];
foreach (Random::sample($population, 3) as $item) {
print($item);
}
// e.g.: c, a, e [random, no repeats]Sample up to $size elements uniformly at random in a single pass (Algorithm R).
Random::reservoirSample(iterable $data, int $size, ?\Random\Engine $engine = null): array
- Does not materialize the full input — only a reservoir of at most
$sizeitems is held in memory, so it is safe over very large (but finite) inputs. An infinite source is invalid: Algorithm R must consume the whole input. - Returns up to
$sizeitems (fewer if the input is shorter). When$size >= countof the input, the entire input is returned in original order and zero random draws occur — this differs deliberately fromsample, which throws\LengthExceptionon oversize$size. - Output keys are sequential 0-indexed.
- Throws
\InvalidArgumentExceptionif$sizeis negative.
use IterTools\Random;
$logLines = $hugeLogFileLineGenerator; // millions of lines, never fully materialized
$reservoir = Random::reservoirSample($logLines, 5);
// e.g.: 5 lines drawn uniformly at random in one pass