Skip to content

Commit

Permalink
replace array_rand() and rand() with random_int() #2
Browse files Browse the repository at this point in the history
  • Loading branch information
gregor-j committed Mar 21, 2022
1 parent ef4f48f commit c53dc0a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/Dictionaries/DictionaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace GregorJ\CorrectHorse\Dictionaries;

use Exception;
use GregorJ\CorrectHorse\DictionaryInterface;
use RuntimeException;

use function array_rand;
use function count;
use function dirname;
use function file;
use function file_exists;
use function random_int;
use function trim;

/**
Expand Down Expand Up @@ -46,14 +48,15 @@ public function __construct(string $filename)
/**
* Get a random word from the dictionary.
* @return string
* @throws Exception
*/
public function getRandomWord(): string
{
$lines = file($this->filename);
$line = '';
while ($line === '') {
$line = trim($lines[array_rand($lines)]);
}
$max = count($lines) - 1;
do {
$line = trim($lines[random_int(0, $max)]);
} while ($line === '');
return $line;
}
}
11 changes: 9 additions & 2 deletions src/Generators/ManageRandomItemsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace GregorJ\CorrectHorse\Generators;

use function array_rand;
use Exception;

use function array_values;
use function count;
use function random_int;
use function shuffle;

/**
Expand All @@ -28,11 +32,14 @@ public function reset(): void
/**
* Remove a random generated item.
* @return void
* @throws Exception
*/
public function remove(): void
{
if ($this->has()) {
unset($this->items[array_rand($this->items)]);
$max = count($this->items) - 1;
unset($this->items[random_int(0, $max)]);
$this->items = array_values($this->items);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Generators/RandomCharacter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace GregorJ\CorrectHorse\Generators;

use Exception;
use GregorJ\CorrectHorse\RandomGeneratorInterface;

use function array_diff;
use function array_rand;
use function array_values;
use function count;
use function in_array;
use function random_int;
use function substr;

/**
Expand Down Expand Up @@ -67,13 +70,15 @@ public function hasChar(string $char): bool
/**
* Add a randomly generated item.
* @return void
* @throws Exception
*/
public function add(): void
{
$chars = array_diff($this->chars, $this->items);
$chars = array_values(array_diff($this->chars, $this->items));
if ($chars === []) {
return;
}
$this->items[] = $chars[array_rand($chars)];
$max = count($chars) - 1;
$this->items[] = $chars[random_int(0, $max)];
}
}
6 changes: 4 additions & 2 deletions src/Generators/RandomNumbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace GregorJ\CorrectHorse\Generators;

use Exception;
use GregorJ\CorrectHorse\RandomGeneratorInterface;

use function count;
use function in_array;
use function rand;
use function random_int;

/**
* Class RandomNumbers
Expand Down Expand Up @@ -81,14 +82,15 @@ private function hasNumber(int $number): bool
/**
* Add a randomly generated item.
* @return void
* @throws Exception
*/
public function add(): void
{
if ($this->isMaxReached()) {
return;
}
do {
$number = rand($this->min, $this->max);
$number = random_int($this->min, $this->max);
} while ($this->hasNumber($number));
$this->items[] = $number;
}
Expand Down
10 changes: 7 additions & 3 deletions src/Generators/RandomWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace GregorJ\CorrectHorse\Generators;

use Exception;
use GregorJ\CorrectHorse\RandomGeneratorInterface;

use function array_merge;
use function rand;
use function random_int;
use function shuffle;

/**
Expand Down Expand Up @@ -38,10 +39,11 @@ public function __construct(RandomGeneratorInterface $lowerCase, RandomGenerator
/**
* Add a randomly generated item.
* @return void
* @throws Exception
*/
public function add(): void
{
if (rand(0, 1) === 0) {
if (random_int(0, 1) === 0) {
$this->lowerCase->add();
} else {
$this->upperCase->add();
Expand All @@ -61,6 +63,7 @@ public function reset(): void
/**
* Remove a random generated item.
* @return void
* @throws Exception
*/
public function remove(): void
{
Expand All @@ -69,7 +72,7 @@ public function remove(): void
} elseif (!$this->lowerCase->has() && $this->upperCase->has()) {
$this->upperCase->remove();
} elseif ($this->upperCase->has() && $this->lowerCase->has()) {
if (rand(0, 1) === 0) {
if (random_int(0, 1) === 0) {
$this->lowerCase->remove();
} else {
$this->upperCase->remove();
Expand All @@ -89,6 +92,7 @@ public function has(): bool
/**
* Get all randomly generated items in random order.
* @return array
* @throws Exception
*/
public function get(): array
{
Expand Down

0 comments on commit c53dc0a

Please sign in to comment.