Skip to content

Commit

Permalink
Removed getRandomValueUrandom() from Helpers class, getUniqueRandomVa…
Browse files Browse the repository at this point in the history
…lues() is now static and uses new PHP's random_int() method
  • Loading branch information
micheleangioni committed May 17, 2017
1 parent 24955cb commit 77c0e94
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 36 deletions.
37 changes: 4 additions & 33 deletions src/MicheleAngioni/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,36 +166,6 @@ static public function daysBetweenDates(string $date1, string $date2):? int

// <<<--- PSEUDO-RANDOM NUMBERS METHODS --->>>

/**
* Return a random value between input $min and $max values by using the MCRYPT_DEV_URANDOM source.
* N.B. Use only on *nix servers!
*
* @param int $min = 0
* @param int $max
*
* @return int
*/
static public function getRandomValueUrandom(int $min = 0, int $max = 0x7FFFFFFF): int
{
if ($max < $min || ($max - $min) > 0x7FFFFFFF) {
return false;
}

$diff = $max - $min;

$bytes = mcrypt_create_iv(4, MCRYPT_DEV_URANDOM);

if ($bytes === false || strlen($bytes) != 4) {
return false;
}

$ary = unpack("Nint", $bytes);
$val = $ary['int'] & 0x7FFFFFFF; // 32-bit safe
$fp = (float)$val / 2147483647.0; // convert to [0,1]

return round($fp * $diff) + $min;
}

/**
* Return $quantity UNIQUE random value between $min and $max.
* Return null on failure.
Expand All @@ -204,9 +174,9 @@ static public function getRandomValueUrandom(int $min = 0, int $max = 0x7FFFFFFF
* @param int $max
* @param int $quantity = 1
*
* @return array|null
* @return int[]|null
*/
public function getUniqueRandomValues(int $min = 0, int $max, int $quantity = 1):? array
static public function getUniqueRandomValues(int $min = 0, int $max, int $quantity = 1):? array
{
if ($min > $max || $quantity < 0) {
return null;
Expand All @@ -215,7 +185,8 @@ public function getUniqueRandomValues(int $min = 0, int $max, int $quantity = 1)
$rand = [];

while (count($rand) < $quantity) {
$r = mt_rand($min, $max);
$r = random_int($min, $max);

if (!in_array($r, $rand)) {
$rand[] = $r;
}
Expand Down
8 changes: 5 additions & 3 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public function testDaysBetweenDates()

public function testGetUniqueRandomValues()
{
$helpers = new H;

$numbers = $helpers->getUniqueRandomValues(1, 100, 4);
$numbers = H::getUniqueRandomValues(1, 100, 4);
$this->assertEquals(4, count($numbers));

$this->assertNotEquals($numbers[0], $numbers[1]);
Expand All @@ -96,6 +94,10 @@ public function testGetUniqueRandomValues()
$this->assertNotEquals($numbers[1], $numbers[2]);
$this->assertNotEquals($numbers[1], $numbers[3]);
$this->assertNotEquals($numbers[2], $numbers[3]);
$this->assertInternalType('int', $numbers[0]);
$this->assertInternalType('int', $numbers[1]);
$this->assertInternalType('int', $numbers[2]);
$this->assertInternalType('int', $numbers[3]);
}

public function tearDown()
Expand Down

0 comments on commit 77c0e94

Please sign in to comment.