PHPRandom
Just another random number/string generator for PHP 5, (*1)
This library is similar to ircmaxell/RandomLib,
except it is compatible with PHP 5.0 and above, and comes with a much simpler interface.
Seriously, PHP ain't Java. Why do we need a dozen files and classes
inheriting one another just to get some random bytes? :p, (*2)
How to Install
Traditional method:, (*3)
include 'phprandom.php';
Composer:, (*4)
"require": {
"kijin/phprandom": "dev-master"
}
How to Use
Get a random integer between two endpoints (inclusive):, (*5)
$random = PHPRandom::getInteger($min, $max);
Get a random floating-point number between 0 and 1:, (*6)
$random = PHPRandom::getFloat();
Get a random alphanumeric string (0-9, a-z, A-Z):, (*7)
$random = PHPRandom::getString($length);
Get a random hexademical string (0-9, a-f):, (*8)
$random = PHPRandom::getHexString($length);
Get a random binary string:, (*9)
$random = PHPRandom::getBinary($length);
Find out where your precious entropy comes from:, (*10)
$sources = PHPRandom::listSources();
Configuration
PHPRandom requires no configuration., (*11)
You don't have to choose whether you want low-quality or high-quality entropy.
You always get the highest quality of entropy that your system supports.
Since the marginal cost of asking for high-quality entropy is negligible
in most cases, there is rarely any need to ask for low-quality entropy., (*12)
Weaker sources of entropy are automatically mixed with other sources
using the best mixing algorithm that your system supports., (*13)
Supported sources, in order of preference:, (*14)
openssl_random_pseudo_bytes()
-
mcrypt_create_iv() with MCRYPT_DEV_URANDOM
-
mcrypt_create_iv() with MCRYPT_RAND (Windows only)
-
/dev/urandom (Linux/Unix only)
-
rand() xor mt_rand()
Supported mixers, in order of preference:, (*15)
-
hash_hmac() with SHA-256
sha1()
License
PHPRandom is free software released under the MIT license., (*16)