Random Number Generation

Zend\Math\Rand implements a random number generator that is able to generate random numbers for general purpose usage and for cryptographic scopes. To generate good random numbers, this component uses different approaches. If PHP 7 is running, we use the cryptographically secure pseudo-random functions random_bytes() and random_int().

For PHP 5 versions, we use paragonie/random_compat, which delegates to the Mcrypt extension or a /dev/urandom or similar source. If you don't have a secure random source in your environment, the functionality will raise an exception, providing hints regarding extensions it can use.

Methods available

The Zend\Math\Rand class offers the following methods to generate random values:

Examples

The example below demonstrates generating random data using Zend\Math\Rand:

use Zend\Math\Rand;

$bytes = Rand::getBytes(32);
printf("Random bytes (in Base64): %s\n", base64_encode($bytes));

$boolean = Rand::getBoolean();
printf("Random boolean: %s\n", $boolean ? 'true' : 'false');

$integer = Rand::getInteger(0, 1000);
printf("Random integer in [0-1000]: %d\n", $integer);

$float = Rand::getFloat();
printf("Random float in [0-1): %f\n", $float);

$string = Rand::getString(32, 'abcdefghijklmnopqrstuvwxyz');
printf("Random string in latin alphabet: %s\n", $string);