From a68fa6694188b84e3eb4b053b0433add4604a7b0 Mon Sep 17 00:00:00 2001 From: David Stensland Date: Thu, 6 Mar 2014 07:55:25 -0500 Subject: [PATCH] Lower randomElements defaults and add readme a note Also tidied up the LengthException message, and made randomElements just a tiny bit faster by moving the array lookup to after a unique random value is chosen. --- readme.md | 1 + src/Faker/Provider/Base.php | 12 ++++++------ test/Faker/Provider/BaseTest.php | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 008777b9..488b39ac 100644 --- a/readme.md +++ b/readme.md @@ -177,6 +177,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle randomNumber($from, $to) // 39049 randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932 randomLetter // 'b' + randomElements($array = array ('a','b','c'), $count = 1) // array('c') randomElement($array = array ('a','b','c')) // 'b' numerify($string = '###') // '609' lexify($string = '????') // 'wgts' diff --git a/src/Faker/Provider/Base.php b/src/Faker/Provider/Base.php index 5a74e2c4..b8ada960 100644 --- a/src/Faker/Provider/Base.php +++ b/src/Faker/Provider/Base.php @@ -132,13 +132,13 @@ class Base * * @return array New array with $count elements from $array */ - public static function randomElements(array $array = array('a', 'b', 'c', 'd', 'e', 'f'), $count = 3) + public static function randomElements(array $array = array('a', 'b', 'c'), $count = 1) { $allKeys = array_keys($array); $numKeys = count($allKeys); if ($numKeys < $count) { - throw new \LengthException("Cannot get $count elements, only " . count($array) . ' in array'); + throw new \LengthException(sprintf('Cannot get %d elements, only %d in array', $count, $numKeys)); } $highKey = $numKeys - 1; @@ -146,13 +146,13 @@ class Base $numElements = 0; while ($numElements < $count) { - $key = $allKeys[mt_rand(0, $highKey)]; - if (isset($keys[$key])) { + $num = mt_rand(0, $highKey); + if (isset($keys[$num])) { continue; } - $keys[$key] = true; - $elements[] = $array[$key]; + $keys[$num] = true; + $elements[] = $array[$allKeys[$num]]; $numElements++; } diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 497c02e4..80fcb0d5 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -241,13 +241,13 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testRandomElements() { - $this->assertCount(3, BaseProvider::randomElements(), 'Should work without any input'); + $this->assertCount(1, BaseProvider::randomElements(), 'Should work without any input'); $empty = BaseProvider::randomElements(array(), 0); $this->assertInternalType('array', $empty); $this->assertCount(0, $empty); - $shuffled = BaseProvider::randomElements(array('foo', 'bar', 'baz')); + $shuffled = BaseProvider::randomElements(array('foo', 'bar', 'baz'), 3); $this->assertContains('foo', $shuffled); $this->assertContains('bar', $shuffled); $this->assertContains('baz', $shuffled);