From 468dd5b726e2c8d4dc015edc3ba2f3c617b99238 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 27 Oct 2012 11:31:55 +0200 Subject: [PATCH] Add numberBetween, and allow randomNumber to receive a min/max value instead of length --- src/Faker/Provider/Base.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Faker/Provider/Base.php b/src/Faker/Provider/Base.php index 7f0bcb59..3bbe1964 100644 --- a/src/Faker/Provider/Base.php +++ b/src/Faker/Provider/Base.php @@ -34,19 +34,41 @@ class Base /** * Returns a random number with 0 to $nbDigits digits * + * If $upTo is passed, it returns a number between $nbDigits (read as from) and $upTo + * * @param integer $nbDigits + * @param integer $upTo * @example 79907610 * * @return integer */ - public static function randomNumber($nbDigits = null) + public static function randomNumber($nbDigits = null, $upTo = null) { if (null === $nbDigits) { $nbDigits = static::randomDigit(); } + + if (null !== $upTo) { + return static::numberBetween($nbDigits, $upTo); + } + return mt_rand(0, pow(10, $nbDigits) - 1); } - + + /** + * Returns a random number between $from and $to + * + * @param integer $from + * @param integer $too + * @example 79907610 + * + * @return integer + */ + public static function numberBetween($from = null, $to = null) + { + return mt_rand($from ?: 0, $to ?: PHP_INT_MAX); + } + /** * Returns a random letter from a to z *