src/Collection/ArrayCollection.php
public function random(int $amount = 1)
    {
        $count = $this->count();
        if ($amount > $count) {
            $message = sprintf('Requested %d items with %d items present', $amount, $count);
            throw new DomainException($message);
        }

        $keys = array_rand($this->items, $amount);

        if ($amount === 1) {
            return $this->items[$keys];
        }

        return new static(array_intersect_key($this->items, array_flip($keys)));
    }
public function random(int $amount = 1)
    {
        $count = $this->count();
        if ($amount > $count) {
            $message = sprintf('Requested %d items with %d items present', $amount, $count);
            throw new DomainException($message);
        }

        $keys = array_rand($this->items, $amount);

        if ($amount === 1) {
            return $this->items[$keys];
        }

        return new static(array_intersect_key($this->items, array_flip($keys)));
    }
public function random ( int $ amount = 1 ) { $ count = $ this -> count ( ) ; if ( $ amount > $ count ) { $ message = sprintf ( 'Requested %d items with %d items present' , $ amount , $ count ) ; throw new DomainException ( $ message ) ; } $ keys = array_rand ( $ this -> items , $ amount ) ; if ( $ amount === 1 ) { return $ this -> items [ $ keys ] ; } return new static ( array_intersect_key ( $ this -> items , array_flip ( $ keys ) ) ) ; }
Retrieves items randomly

@param int $amount The number of items

@return mixed

@throws DomainException When amount is greater than item count
Retrieves items randomly