ZetCode

PHP array_rand Function

last modified March 13, 2025

The PHP array_rand function picks one or more random keys from an array. It's useful for selecting random elements without shuffling.

Basic Definition

The array_rand function returns random keys from an array. By default, it returns a single key. You can specify how many keys to return.

Syntax: array_rand(array $array, int $num = 1): int|string|array. The function returns a single key or an array of keys for multiple selections.

Basic array_rand Example

This shows how to get a single random key from an array of colors.

basic_array_rand.php
<?php

$colors = ["red", "green", "blue", "yellow", "orange"];
$randomKey = array_rand($colors);

echo "Random color: " . $colors[$randomKey];

This picks one random key from the colors array. The key is used to access and display the corresponding color value. Each run may show a different color.

Getting Multiple Random Elements

You can specify how many random keys to return by providing the second parameter.

multiple_elements.php
<?php

$fruits = ["apple", "banana", "cherry", "date", "elderberry"];
$randomKeys = array_rand($fruits, 3);

echo "Random fruits:\n";
foreach ($randomKeys as $key) {
    echo $fruits[$key] . "\n";
}

This selects three random keys from the fruits array. The keys are returned as an array, which we then use to access and display the corresponding fruit names.

Working with Associative Arrays

array_rand works with associative arrays, returning random keys.

associative_array.php
<?php

$capitals = [
    "France" => "Paris",
    "Germany" => "Berlin",
    "Italy" => "Rome",
    "Spain" => "Madrid"
];

$randomCountry = array_rand($capitals);
echo "Random capital: $capitals[$randomCountry] of $randomCountry";

This selects a random key (country name) from the associative array. We then display both the country and its capital using the randomly selected key.

Handling Edge Cases

When requesting more elements than exist, array_rand throws a warning.

edge_case.php
<?php

$letters = ["a", "b", "c"];

// This will trigger a warning if uncommented:
// $randomKeys = array_rand($letters, 5);

// Safe approach:
$count = min(3, count($letters));
$randomKeys = array_rand($letters, $count);

print_r($randomKeys);

This demonstrates handling the case where you might request more elements than available. The safe approach limits the request to the array's actual size.

Seeding for Reproducible Results

For testing, you can seed the random number generator to get consistent results.

seeding_random.php
<?php

$items = ["rock", "paper", "scissors"];

// Seed the random number generator for reproducible results
mt_srand(42);
$firstRun = array_rand($items);

mt_srand(42); // Reset with same seed
$secondRun = array_rand($items);

echo "First run: $items[$firstRun]\n";
echo "Second run: $items[$secondRun]\n"; // Same as first run

By seeding the random number generator with the same value, we get identical "random" results. This is useful for testing but shouldn't be used in production.

Best Practices

Source

PHP array_rand Documentation

This tutorial covered the PHP array_rand function with practical examples showing its usage for selecting random array elements.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Array Functions.