parent
bd307c8081
commit
bacc3323d5
@ -0,0 +1,106 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Robert@TryingToScale.com> |
||||||
|
* @copyright Copyright (c) 2022, Robert Strutts. |
||||||
|
* @license https://mit-license.org/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace tts; |
||||||
|
|
||||||
|
/** |
||||||
|
* random_engine - Provides a high-level API to the randomness |
||||||
|
* provided by an Random\Engine OR uses the next Fall Back FN. |
||||||
|
* |
||||||
|
* @author Robert Strutts <Robert@TryingToScale.com> |
||||||
|
*/ |
||||||
|
class random_engine { |
||||||
|
|
||||||
|
private $engine = false; |
||||||
|
|
||||||
|
public function __construct(): string { |
||||||
|
$version = (float) phpversion(); |
||||||
|
if ($version > 8.1) { |
||||||
|
$this->engine = new \Random\Randomizer(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function get_bytes(int $bytes_length = 16): string { |
||||||
|
return ($this->engine) ? $this->engine->getBytes($bytes_length) : |
||||||
|
random_bytes($bytes_length); |
||||||
|
} |
||||||
|
|
||||||
|
public function get_int(int $min, int $max): int { |
||||||
|
if ($this->engine) { |
||||||
|
return $this->engine->getInt($min, $max); |
||||||
|
} |
||||||
|
if (function_exists('random_int')) { |
||||||
|
return random_int($min, $max); // secure fallback |
||||||
|
} elseif (function_exists('mt_rand')) { |
||||||
|
return mt_rand($min, $max); // fast |
||||||
|
} |
||||||
|
return rand($min, $max); // old |
||||||
|
} |
||||||
|
|
||||||
|
// Took from source https://pageconfig.com/post/fixed-length-large-random-numbers-with-php |
||||||
|
private function big_rand(int $len = 18 ): int { |
||||||
|
$rand = ''; |
||||||
|
while( !( isset( $rand[$len-1] ) ) ) { |
||||||
|
$rand .= mt_rand( ); |
||||||
|
} |
||||||
|
return (int) substr( $rand , 0 , $len ); |
||||||
|
} |
||||||
|
|
||||||
|
public function get_next_big_postive_int(): int { |
||||||
|
if ($this->engine) { |
||||||
|
return $this->engine->nextInt(); |
||||||
|
} |
||||||
|
return $this->big_rand(); |
||||||
|
} |
||||||
|
|
||||||
|
private function select_from_array(array $a, int $num ): array { |
||||||
|
$array_count = \bs_tts\common::get_count($a) - 1; |
||||||
|
if ($array_count < 1) { |
||||||
|
return []; |
||||||
|
} |
||||||
|
$ret = []; |
||||||
|
for($i=0; $i<$num; $i++) { |
||||||
|
$ret[] = $a[$this->get_int(0, $array_count)]; |
||||||
|
} |
||||||
|
return $ret; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Pick random keys from an Array |
||||||
|
*/ |
||||||
|
public function get_array_keys(array $a, int $num): array { |
||||||
|
if ($this->engine) { |
||||||
|
return $this->engine->pickArrayKeys($a, $num); |
||||||
|
} |
||||||
|
return $this->select_from_array($a, $num); |
||||||
|
} |
||||||
|
|
||||||
|
public function get_shuffled_array(array $a): array { |
||||||
|
if ($this->engine) { |
||||||
|
return $this->engine->shuffleArray($a); |
||||||
|
} |
||||||
|
shuffle($a); |
||||||
|
return $a; |
||||||
|
} |
||||||
|
|
||||||
|
public function get_shuffled_bytes(string $bytes): string { |
||||||
|
if ($this->engine) { |
||||||
|
return $this->engine->shuffleBytes($bytes); |
||||||
|
} |
||||||
|
$len = mb_strlen($bytes); |
||||||
|
$a = []; |
||||||
|
while($len-- > 0) { |
||||||
|
$a[] = mb_substr($bytes, $len, 1); |
||||||
|
} |
||||||
|
shuffle($a); |
||||||
|
return join('', $a); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue