Skip to content

Commit

Permalink
Enhancement: Use Helper to select random number between minimum and m…
Browse files Browse the repository at this point in the history
…aximum (#757)
  • Loading branch information
localheinz authored Sep 14, 2023
1 parent 6a17f46 commit 57e1f99
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/Faker/ChanceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Faker;

use Faker\Extension\Extension;

/**
* This generator returns a default value for all called properties
* and methods. It works with Faker\Generator::optional().
Expand All @@ -17,7 +15,7 @@ class ChanceGenerator
protected $default;

/**
* @param Extension|Generator $generator
* @param Extension\Extension|Generator $generator
*/
public function __construct($generator, float $weight, $default = null)
{
Expand Down Expand Up @@ -51,7 +49,7 @@ public function __get($attribute)
*/
public function __call($name, $arguments)
{
if (mt_rand(1, 100) <= (100 * $this->weight)) {
if (Extension\Helper::randomNumberBetween(1, 100) <= (100 * $this->weight)) {
return call_user_func_array([$this->generator, $name], $arguments);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Faker/Core/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function numberBetween(int $min = 0, int $max = 2147483647): int
$int1 = min($min, $max);
$int2 = max($min, $max);

return mt_rand($int1, $int2);
return Extension\Helper::randomNumberBetween($int1, $int2);
}

public function randomDigit(): int
Expand Down
13 changes: 9 additions & 4 deletions src/Faker/Extension/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public static function randomNumber(): int
return mt_rand();
}

public static function randomNumberBetween(int $min, int $max): int
{
return mt_rand($min, $max);
}

public static function largestRandomNumber(): int
{
return mt_getrandmax();
Expand Down Expand Up @@ -58,7 +63,7 @@ public static function numerify(string $string): string

while ($i < $nbReplacements) {
$size = min($nbReplacements - $i, $maxAtOnce);
$numbers .= str_pad((string) mt_rand(0, 10 ** $size - 1), $size, '0', STR_PAD_LEFT);
$numbers .= str_pad((string) self::randomNumberBetween(0, 10 ** $size - 1), $size, '0', STR_PAD_LEFT);
$i += $size;
}

Expand All @@ -68,7 +73,7 @@ public static function numerify(string $string): string
}

return self::replaceWildcard($string, '%', static function () {
return mt_rand(1, 9);
return self::randomNumberBetween(1, 9);
});
}

Expand All @@ -80,7 +85,7 @@ public static function numerify(string $string): string
public static function lexify(string $string): string
{
return self::replaceWildcard($string, '?', static function () {
return chr(mt_rand(97, 122));
return chr(self::randomNumberBetween(97, 122));
});
}

Expand All @@ -93,7 +98,7 @@ public static function lexify(string $string): string
public static function bothify(string $string): string
{
$string = self::replaceWildcard($string, '*', static function () {
return mt_rand(0, 1) === 1 ? '#' : '?';
return self::randomNumberBetween(0, 1) === 1 ? '#' : '?';
});

return self::lexify(self::numerify($string));
Expand Down
26 changes: 13 additions & 13 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(Generator $generator)
*/
public static function randomDigit()
{
return mt_rand(0, 9);
return Extension\Helper::randomNumberBetween(0, 9);
}

/**
Expand All @@ -42,7 +42,7 @@ public static function randomDigit()
*/
public static function randomDigitNotNull()
{
return mt_rand(1, 9);
return Extension\Helper::randomNumberBetween(1, 9);
}

/**
Expand Down Expand Up @@ -91,10 +91,10 @@ public static function randomNumber($nbDigits = null, $strict = false)
}

if ($strict) {
return mt_rand(10 ** ($nbDigits - 1), $max);
return Extension\Helper::randomNumberBetween(10 ** ($nbDigits - 1), $max);
}

return mt_rand(0, $max);
return Extension\Helper::randomNumberBetween(0, $max);
}

/**
Expand Down Expand Up @@ -146,7 +146,7 @@ public static function numberBetween($int1 = 0, $int2 = 2147483647)
$min = $int1 < $int2 ? $int1 : $int2;
$max = $int1 < $int2 ? $int2 : $int1;

return mt_rand($min, $max);
return Extension\Helper::randomNumberBetween($min, $max);
}

/**
Expand All @@ -164,7 +164,7 @@ public static function passthrough($value)
*/
public static function randomLetter()
{
return chr(mt_rand(97, 122));
return chr(Extension\Helper::randomNumberBetween(97, 122));
}

/**
Expand All @@ -174,7 +174,7 @@ public static function randomLetter()
*/
public static function randomAscii()
{
return chr(mt_rand(33, 126));
return chr(Extension\Helper::randomNumberBetween(33, 126));
}

/**
Expand Down Expand Up @@ -223,7 +223,7 @@ public static function randomElements($array = ['a', 'b', 'c'], $count = 1, $all
}

if (null === $count) {
$count = mt_rand(1, $numberOfElements);
$count = Extension\Helper::randomNumberBetween(1, $numberOfElements);
}

$randomElements = [];
Expand All @@ -234,7 +234,7 @@ public static function randomElements($array = ['a', 'b', 'c'], $count = 1, $all
$numberOfRandomElements = 0;

while ($numberOfRandomElements < $count) {
$index = mt_rand(0, $maxIndex);
$index = Extension\Helper::randomNumberBetween(0, $maxIndex);

if (!$allowDuplicates) {
if (isset($elementHasBeenSelectedAlready[$index])) {
Expand Down Expand Up @@ -305,7 +305,7 @@ public static function randomKey($array = [])
}
$keys = array_keys($array);

return $keys[mt_rand(0, count($keys) - 1)];
return $keys[Extension\Helper::randomNumberBetween(0, count($keys) - 1)];
}

/**
Expand Down Expand Up @@ -362,7 +362,7 @@ public static function shuffleArray($array = [])
if ($i == 0) {
$j = 0;
} else {
$j = mt_rand(0, $i);
$j = Extension\Helper::randomNumberBetween(0, $i);
}

if ($j == $i) {
Expand Down Expand Up @@ -492,7 +492,7 @@ public static function lexify($string = '????')
public static function bothify($string = '## ??')
{
$string = self::replaceWildcard($string, '*', static function () {
return mt_rand(0, 1) === 1 ? '#' : '?';
return Extension\Helper::randomNumberBetween(0, 1) === 1 ? '#' : '?';
});

return static::lexify(static::numerify($string));
Expand Down Expand Up @@ -648,7 +648,7 @@ public function optional($weight = 0.5, $default = null)
}

// new system with percentage
if (is_int($weight) && mt_rand(1, 100) <= $weight) {
if (is_int($weight) && Extension\Helper::randomNumberBetween(1, 100) <= $weight) {
return $this->generator;
}

Expand Down
8 changes: 5 additions & 3 deletions src/Faker/Provider/ar_EG/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider\ar_EG;

use Faker\Extension;

class Person extends \Faker\Provider\Person
{
protected static $maleNameFormats = [
Expand Down Expand Up @@ -86,12 +88,12 @@ public static function prefix()
*/
public static function nationalIdNumber($gender = null)
{
$randomBirthDateTimestamp = mt_rand(strtotime('1950-Jan-10'), strtotime('2005-Dec-25'));
$randomBirthDateTimestamp = Extension\Helper::randomNumberBetween(strtotime('1950-Jan-10'), strtotime('2005-Dec-25'));

$centuryId = ((int) date('Y', $randomBirthDateTimestamp)) >= 2000 ? 3 : 2;
$fullBirthDate = date('ymd', $randomBirthDateTimestamp);
$governorateId = Address::governorateId();
$birthRegistrationSequence = mt_rand(1, 500);
$birthRegistrationSequence = Extension\Helper::randomNumberBetween(1, 500);

if ($gender === static::GENDER_MALE) {
$birthRegistrationSequence = $birthRegistrationSequence | 1; // Convert to the nearest odd number
Expand All @@ -100,7 +102,7 @@ public static function nationalIdNumber($gender = null)
}

$birthRegistrationSequence = str_pad((string) $birthRegistrationSequence, 4, '0', STR_PAD_LEFT);
$randomCheckDigit = mt_rand(1, 9);
$randomCheckDigit = Extension\Helper::randomNumberBetween(1, 9);

return $centuryId . $fullBirthDate . $governorateId . $birthRegistrationSequence . $randomCheckDigit;
}
Expand Down

0 comments on commit 57e1f99

Please sign in to comment.