Core\Random::sample
Core\Random::sample(array<T> $a, uint $count): array<T>Core\Random::sample( array<T> $a, uint $count, ): array<T>
Chooses several different values from an array at random, for example three winners of a prize draw or five questions for a quiz.
Core\Random::sample takes the array and the number of values you want. It returns a new list with that many values. Each entry of the array is chosen at most once. Every possible choice has the same chance. The values come back in a random order, and their keys start again at 0. The array itself does not change. If you ask for more values than the array has, Core\Random::sample throws a RuntimeError. The choice comes from a cryptographic random generator, so nobody can predict it.
Good to know: to put every value in a random order, use Core\Random::shuffle.
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 random order, with the keys 0, 1, …. The keys of $a are not kept, and $a is unchanged.
Errors
RuntimeError$countis greater than the number of entries in$a.
Examples
Three winners
<?nvs// Shows how to choose three different winners from a list of names.
array<string> $names = ['Ana', 'Ben', 'Chen', 'Dara', 'Emil', 'Fay'];
// `Core\Random::sample` returns three of the names. No name is chosen twice.array<string> $winners = Core\Random::sample($names, 3);
// The winners change on every run, so this prints only what is always true: 3 and 3.echo Core\Arr::count($winners), "\n";echo Core\Arr::count(Core\Arr::unique($winners)), "\n";
// Every winner is one of the names. This prints `yes` three times.foreach ($winners as string $winner) { echo Core\Arr::contains($names, $winner) ? 'yes' : 'no', "\n";}
// The list of names does not change. This prints 6.echo Core\Arr::count($names), "\n";33yesyesyes6More 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'];
// A count of 0 returns an empty list. This prints [].echo Core\Json::encode(Core\Random::sample($colors, 0)), "\n";
// A count of 3 returns every color, in a random order. This prints 3.echo Core\Arr::count(Core\Random::sample($colors, 3)), "\n";
// The array has only three colors, so a count of 4 throws an error.try { echo Core\Json::encode(Core\Random::sample($colors, 4)), "\n";} catch (Throwable $e) { echo 'Error: ', $e->message, "\n";}[]3Error: Core\Random::sample(): asked for 4 distinct entries from an array that holds 3Questions for a quiz
<?nvs// Shows how to build a quiz from five questions chosen at random from a larger set.
array<string> $questions = [ 'q1' => 'What is 2 + 2?', 'q2' => 'What is the capital of France?', 'q3' => 'How many days are in a week?', 'q4' => 'What color is the sky on a clear day?', 'q5' => 'How many legs does a spider have?', 'q6' => 'What is the boiling point of water in Celsius?', 'q7' => 'How many minutes are in an hour?', 'q8' => 'What is 10 divided by 2?',];
// The result is a list of five question texts. The keys "q1" to "q8" are not kept.array<string> $quiz = Core\Random::sample($questions, 5);
// The questions change on every run. This prints the numbers 1 to 5, each with `ok`.int $number = 1;foreach ($quiz as string $question) { echo $number, '. ', Core\Arr::contains($questions, $question) ? 'ok' : 'missing', "\n"; $number = $number + 1;}
// Every student gets five different questions. This prints 5.echo Core\Arr::count(Core\Arr::unique($quiz)), "\n";1. ok2. ok3. ok4. ok5. ok5