Skip to content

Commit

Permalink
Enhancement: Use Helper to determine largest random number (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Sep 14, 2023
1 parent 51794a1 commit b399ff3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Faker/Core/Number.php
Expand Up @@ -60,7 +60,7 @@ public function randomFloat(?int $nbMaxDecimals = null, float $min = 0, ?float $
$max = $tmp;
}

return round($min + $this->numberBetween() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
return round($min + $this->numberBetween() / Extension\Helper::largestRandomNumber() * ($max - $min), $nbMaxDecimals);
}

public function randomNumber(int $nbDigits = null, bool $strict = false): int
Expand All @@ -70,7 +70,7 @@ public function randomNumber(int $nbDigits = null, bool $strict = false): int
}
$max = 10 ** $nbDigits - 1;

if ($max > mt_getrandmax()) {
if ($max > Extension\Helper::largestRandomNumber()) {
throw new \InvalidArgumentException('randomNumber() can only generate numbers up to mt_getrandmax()');
}

Expand Down
7 changes: 6 additions & 1 deletion src/Faker/Extension/Helper.php
Expand Up @@ -21,6 +21,11 @@ public static function randomElement(array $array)
return $array[array_rand($array, 1)];
}

public static function largestRandomNumber(): int
{
return mt_getrandmax();
}

/**
* Replaces all hash sign ('#') occurrences with a random number
* Replaces all percentage sign ('%') occurrences with a non-zero number.
Expand All @@ -42,7 +47,7 @@ public static function numerify(string $string): string
}

if ($nbReplacements = count($toReplace)) {
$maxAtOnce = strlen((string) mt_getrandmax()) - 1;
$maxAtOnce = strlen((string) self::largestRandomNumber()) - 1;
$numbers = '';
$i = 0;

Expand Down
9 changes: 5 additions & 4 deletions src/Faker/Provider/Base.php
Expand Up @@ -3,6 +3,7 @@
namespace Faker\Provider;

use Faker\DefaultGenerator;
use Faker\Extension;
use Faker\Generator;
use Faker\UniqueGenerator;
use Faker\ValidGenerator;
Expand Down Expand Up @@ -85,7 +86,7 @@ public static function randomNumber($nbDigits = null, $strict = false)
}
$max = 10 ** $nbDigits - 1;

if ($max > mt_getrandmax()) {
if ($max > Extension\Helper::largestRandomNumber()) {
throw new \InvalidArgumentException('randomNumber() can only generate numbers up to mt_getrandmax()');
}

Expand Down Expand Up @@ -127,7 +128,7 @@ public static function randomFloat($nbMaxDecimals = null, $min = 0, $max = null)
$max = $tmp;
}

return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
return round($min + mt_rand() / Extension\Helper::largestRandomNumber() * ($max - $min), $nbMaxDecimals);
}

/**
Expand Down Expand Up @@ -449,7 +450,7 @@ public static function numerify($string = '###')
}

if ($nbReplacements = count($toReplace)) {
$maxAtOnce = strlen((string) mt_getrandmax()) - 1;
$maxAtOnce = strlen((string) Extension\Helper::largestRandomNumber()) - 1;
$numbers = '';
$i = 0;

Expand Down Expand Up @@ -642,7 +643,7 @@ public function optional($weight = 0.5, $default = null)
{
// old system based on 0.1 <= $weight <= 0.9
// TODO: remove in v2
if ($weight > 0 && $weight < 1 && mt_rand() / mt_getrandmax() <= $weight) {
if ($weight > 0 && $weight < 1 && mt_rand() / Extension\Helper::largestRandomNumber() <= $weight) {
return $this->generator;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Faker/Provider/Biased.php
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider;

use Faker\Extension;

class Biased extends Base
{
/**
Expand All @@ -23,8 +25,8 @@ class Biased extends Base
public function biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt')
{
do {
$x = mt_rand() / mt_getrandmax();
$y = mt_rand() / (mt_getrandmax() + 1);
$x = mt_rand() / Extension\Helper::largestRandomNumber();
$y = mt_rand() / (Extension\Helper::largestRandomNumber() + 1);
} while (call_user_func($function, $x) < $y);

return (int) floor($x * ($max - $min + 1) + $min);
Expand Down

0 comments on commit b399ff3

Please sign in to comment.