Skip to content

Core\Random\Seeded::sample

$seeded->sample(array<T> $a, uint $count): array<T>$seeded->sample(
    array<T> $a,
    uint $count,
): array<T>

Chooses several different values from an array with a seeded generator, so the same values come back on every run.

You make the generator with new Core\Random\Seeded(42). The number 42 is the seed. Two generators with the same seed choose the same values in the same order. sample takes the array and the number of values you want, and returns a new list with that many values. Each entry of the array is chosen at most once. The keys of the new list start again at 0, and the array does not change.

If you ask for more values than the array has, sample throws an error.

Good to know: anybody who knows the seed knows every choice. For a prize draw, use Core\Random::sample.

Parameters

  • $aarray<T>The array to choose from.
  • $countuintHow many different entries to return. It must not be more than the entries in $a.

Return value

Returns array<T>. A new list of the chosen values, in the order they were chosen, with the keys 0, 1, …. The keys of $a are not kept, and $a is unchanged.

Errors

  • RuntimeError$count is greater than the number of entries in $a.

Examples

The same seed gives the same values

<?nvs
// Shows that two generators with the same seed choose the same three values.
array<string> $names = ['Ana', 'Ben', 'Chen', 'Dara', 'Emil', 'Fay'];
Core\Random\Seeded $first = new Core\Random\Seeded(42);
Core\Random\Seeded $second = new Core\Random\Seeded(42);
// Both lines are the same, and no name is in a line twice.
echo Core\Json::encode($first->sample($names, 3)), "\n";
echo Core\Json::encode($second->sample($names, 3)), "\n";
// The next call returns the next choice, so this line can be different.
echo Core\Json::encode($first->sample($names, 3)), "\n";
Output
["Emil","Ana","Ben"]
["Emil","Ana","Ben"]
["Chen","Ana","Fay"]

More than the array has

<?nvs
// Shows what happens when you ask for none of the values, and for more values than the array has.
array<string> $colors = ['red', 'green', 'blue'];
Core\Random\Seeded $generator = new Core\Random\Seeded(42);
// A count of 0 returns an empty list. This prints [].
echo Core\Json::encode($generator->sample($colors, 0)), "\n";
// The array has only three colors, so a count of 4 throws an error.
try {
echo Core\Json::encode($generator->sample($colors, 4)), "\n";
} catch (Throwable $e) {
echo 'Error: ', $e->message, "\n";
}
Output
[]
Error: Core\Random\Seeded::sample(): asked for 4 distinct entries from an array that holds 3

Products for a smoke test

<?nvs
// Shows how to choose a few products for a quick test, with the same choice on every run.
// A shop has many products, and a quick test checks only three of their pages.
// The seed is fixed, so a failed test checks the same three pages when it runs again.
array<string> $products = [
'mug',
't-shirt',
'poster',
'notebook',
'pen',
'sticker',
'cap',
'bag',
];
Core\Random\Seeded $generator = new Core\Random\Seeded(2024);
foreach ($generator->sample($products, 3) as string $product) {
echo 'check https://example.com/shop/', $product, "\n";
}
Output
check https://example.com/shop/pen
check https://example.com/shop/mug
check https://example.com/shop/poster