Installation:
composer require arokettu/random-polyfill
ext-random if available.First Use Case: Generate a cryptographically secure random integer between 1 and 100:
use Random\Engine\Secure;
use Random\Randomizer;
$engine = new Secure();
$randomizer = new Randomizer($engine);
$randomInt = $randomizer->getInt(1, 100);
Where to Look First:
src/Randomizer.php: Core randomness logic.src/Engine/: All available PRNG engines (e.g., Secure.php, Mt19937.php).rand()/mt_rand)use Random\Engine\Mt19937;
use Random\Randomizer;
$engine = new Mt19937();
$randomizer = new Randomizer($engine);
// Replace rand(1, 100)
$value = $randomizer->getInt(1, 100);
// Replace mt_rand()
$value = $randomizer->getInt();
use Random\Engine\Secure;
use Random\Randomizer;
$engine = new Secure();
$randomizer = new Randomizer($engine);
// Generate a secure token
$token = bin2hex($randomizer->getBytes(16));
// Shuffle an array securely
$array = [1, 2, 3, 4, 5];
$randomizer->shuffleArray($array);
use Random\Engine\PcgOneseq128XslRr64;
use Random\Randomizer;
$engine = new PcgOneseq128XslRr64();
$randomizer = new Randomizer($engine);
// Faster than Mt19937 for high-throughput scenarios
$randomInt = $randomizer->getInt();
$randomizer = new Randomizer(new Secure());
$serialized = serialize($randomizer);
$unserialized = unserialize($serialized); // Works in PHP 7.4+
rand()/mt_randrand($min, $max) → $randomizer->getInt($min, $max)mt_rand() → $randomizer->getInt()$engine = new Mt19937();
$engine->seed(time()); // Optional: Explicit seeding
$randomizer = new Randomizer($engine);
getInt(1, PHP_INT_MAX)).// app/Providers/AppServiceProvider.php
use Illuminate\Support\ServiceProvider;
use Random\Engine\Secure;
use Random\Randomizer;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(Randomizer::class, function ($app) {
return new Randomizer(new Secure());
});
}
}
use Random\Randomizer;
class UserController extends Controller
{
public function __construct(private Randomizer $randomizer) {}
public function generateToken()
{
return bin2hex($this->randomizer->getBytes(16));
}
}
// app/Helpers/RandomHelper.php
if (!function_exists('random_bytes')) {
function random_bytes(int $length): string
{
$randomizer = app(Randomizer::class);
return $randomizer->getBytes($length);
}
}
Extend the Random\Engine interface for domain-specific needs:
use Random\Engine;
use Random\Engine\Secure;
class CustomEngine implements Engine
{
public function getBytes(int $length): string
{
// Implement custom logic (e.g., combine Secure + Mt19937)
$secure = new Secure();
return $secure->getBytes($length);
}
public function nextInt(): int
{
// Implement custom logic
return (int) hexdec(bin2hex($this->getBytes(4)));
}
}
PHP 8.2+ Fallback:
The package automatically uses native ext-random if available. No manual checks are needed.
Performance Considerations:
Secure engine.Mt19937 or PcgOneseq128XslRr64 (faster on 64-bit systems).getBytes() in loops: Pre-generate bytes if possible (e.g., getBytes(1000) once, then slice).Thread Safety: All engines are thread-safe (stateless or internally synchronized). Safe for use in concurrent environments (e.g., Laravel queues).
Testing:
Mt19937 with a fixed seed for deterministic tests:
$engine = new Mt19937();
$engine->seed(42); // Fixed seed
$randomizer = new Randomizer($engine);
getInt(PHP_INT_MIN, PHP_INT_MAX).getBytes(0) or getBytes(PHP_INT_MAX).Legacy PHP 7.1:
Mt19937) may have reduced performance or integer overflow risks. Use PcgOneseq128XslRr64 as an alternative.Serialization Warnings:
arrayPickKeys() may throw warnings in non-cryptographic engines (e.g., Mt19937). Suppress with:
$randomizer->arrayPickKeys($array, $num, false); // Disable warning
Secure engine if you need arrayPickKeys() without warnings.Integer Overflow in 32-bit PHP:
Mt19937 may produce incorrect results for very large ranges (e.g., getInt(1, PHP_INT_MAX)).PcgOneseq128XslRr64 or constrain ranges to < 2^31.PHP 7.1 Limitations:
random_int(): This polyfill does not replace random_int(). Use Secure engine + getInt() instead.Engine-Specific Quirks:
Secure engine throws RandomException (not Exception) for errors.Xoshiro256StarStar is faster than Mt19937 but less random for cryptographic use.PHP 8.2+ Auto-Detection:
putenv('PHP_RANDOM_POLYFILL_FORCE=1');
Verify Engine Behavior:
$engine = new Secure();
$bytes = $engine->getBytes(16);
var_dump(hex2bin($bytes)); // Should be 16 truly random bytes
**Check for Side Effects
How can I help you explore Laravel packages today?