RandoPHP is a PHP open source package for random stuff. With this package you can:
- Draw: Extract random items (sample) from an array. This is useful when you want to "draw" some numbers or items;
- Generate: useful for create random
- item like integer, byte, boolean, float, lat/long coordinates, char (numeric, alphabetic, alphanumeric);
- sequences like array of integer or char;
With the fluent interface you can control some things like:
- minimum and maximum value for generation;
- how many items you want to create;
- for sequences if you want or not duplicates ([1,5,3,1,1], 1 is duplicate or [1,6,5,3,8], no duplicates);
- And other stuff, see the documentation for more options.
You can install the package via composer:
composer require hi-folks/rando-phpSometimes you want to obtain a random char, for example, a numeric char:
Randomize::char()->numeric()->generate();Or you might want an alphabetic char:
Randomize::char()->alpha()->generate();You can even do both!
Randomize::char()->alphanumeric()->generate();Sometimes you want to obtain a random boolean true or false (flip a coin):
$randomBool = Randomize::boolean()->generate();Sometimes you want to obtain a random float (default min - max range of 0.0 - 1.0). For example, you want to generate a random temperature for a day:
$randomFloat = Randomize::float()->generate();Or you can set the min - max range of 0 - 90 , which is equivalent to ->min(0)->max(90)
$randomFloat = Randomize::float()->min(0)->max(90)->generate();Sometimes you want to obtain a random integer (min - max range). For example, you want to roll the dice:
$randomNumber = Randomize::integer()->min(1)->max(6)->generate();The same thing using range() method, instead of min() and max():
$randomNumber = Randomize::integer()->range(1,6)->generate();Sometime you want to obtain some random bytes (hexadecimal). For example, you want to generate a random RGB color (a hex triplet in hexadecimal format):
$randomRGB = Randomize::byte()->length(3)->generate();Sometimes you want to obtain a random date (default min - max range of First day of current year - Last day of current year). For example, you want to generate a random date:
$randomDate = Randomize::datetime()->generate();Or you can set the min - max range of 01-01-2020 - 10-01-2020 , which is equivalent to ->min('01-01-2020')->max('10-01-2020'):
$randomDate = Randomize::datetime()->min('01-01-2020')->max('10-01-2020')->generate();You can even specify your preferred format for the random date generated, by using ->format('d-M-Y'):
$randomDate = Randomize::datetime()->format('d-M-Y')->generate();Sometime you want to obtain some random sequences. For example, you want to roll the dice 15 times:
$randomRolls = Randomize::sequence()->min(1)->max(6)->count(15)->generate();Sometime you want to obtain some random char sequences. For example, char sequences of length 10:
$randomChars = Randomize::sequence()->chars()->count(10)->generate();Or you might want numeric char sequences.
$randomChars = Randomize::sequence()->chars()->numeric()->count(10)->generate();Or you might want alphabetical char sequences.
$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->generate();Yes, even both.
$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->generate();Sometime you want to obtain some random sequences with no duplicates. For example, you want to play "Tombola" (extracting number from 1 to 90 with NO duplicates):
$randomTombola = Randomize::sequence()->min(1)->max(90)->count(90)->noDuplicates()->generate();Sometime you want to obtain some random char sequences with no duplicates.
$randomChars = Randomize::sequence()->chars()->count(10)->noDuplicates()->generate();Or you might want numeric char sequences with no duplicates. For example, char sequences of length 10:
$randomChars = Randomize::sequence()->chars()->numeric()->count(10)->noDuplicates()->generate();Or you might want alphabetical char sequences with no duplicates.
$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->noDuplicates()->generate();Yes, even both and with no duplicates.
$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->noDuplicates()->generate();If you have a list of values and you want to extract/select/draw one or more elements, you could use Draw class instead of Randomize.
$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->extract();$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->count(3)->extract();$array = ["React.js", "Vue.js", "Svelte.js", "Angular.js", "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->count(3)->allowDuplicates()->extract();composer testIf you want to see some coverage report you can execute phpunit with coverage-text option:
vendor/bin/phpunit --coverage-textUnder the hood RandoPHP uses some native PHP functions like:
- array_rand(): PHP Doc for array_rand;
- random_int(): PHP Doc for random_int;
- shuffle(): PHP Doc for shuffle;
- random_bytes(): PHP Doc for random_bytes.
These PHP functions use a pseudo random number generator that is not suitable for cryptographic purposes.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
Take a look if your request is already there https://github.com/Hi-Folks/rando-php/issues If it is not present, you can create a new one https://github.com/Hi-Folks/rando-php/issues/new
The MIT License (MIT). Please see License File for more information.
