From 176507cc2664e67700e3460d3e54afd2a546f5f1 Mon Sep 17 00:00:00 2001
From: David Stensland <me@terite.com>
Date: Wed, 5 Mar 2014 13:36:24 -0500
Subject: [PATCH] make randomElements ~12% faster

---
 src/Faker/Provider/Base.php | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/Faker/Provider/Base.php b/src/Faker/Provider/Base.php
index 0210b217..5a74e2c4 100644
--- a/src/Faker/Provider/Base.php
+++ b/src/Faker/Provider/Base.php
@@ -134,24 +134,29 @@ class Base
      */
     public static function randomElements(array $array = array('a', 'b', 'c', 'd', 'e', 'f'), $count = 3)
     {
-        if (count($array) < $count) {
+        $allKeys = array_keys($array);
+        $numKeys = count($allKeys);
+
+        if ($numKeys < $count) {
             throw new \LengthException("Cannot get $count elements, only " . count($array) . ' in array');
         }
-        $keys = array_keys($array);
-        $highKey = count($keys) - 1;
+
+        $highKey = $numKeys - 1;
+        $keys = $elements = array();
         $numElements = 0;
 
-        $elements = array();
         while ($numElements < $count) {
-            $key = $keys[mt_rand(0, $highKey)];
-            if (isset($elements[$key])) {
+            $key = $allKeys[mt_rand(0, $highKey)];
+            if (isset($keys[$key])) {
                 continue;
             }
-            $elements[$key] = $array[$key];
+
+            $keys[$key] = true;
+            $elements[] = $array[$key];
             $numElements++;
         }
 
-        return array_values($elements);
+        return $elements;
     }
 
     /**