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;
- sequences like array of integer;
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-php
Sometimes you want to obtain a random char, for example, a numeric char:
Randomize::char()->numeric()->generate();
Or you might want a 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 integer (min - max rang). For example, you want to roll the dice:
$randomNumber = Randomize::integer()->min(1)->max(6)->generate();
Set the range (min and max) Calling range(1,10), it is equivalent of ->min(1)->max(10):
$randomNumber = Randomize::integer()->range(1,10)->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();
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 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();
$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 test
If you want to see some coverage report you can execute phpunit with coverage-text option:
vendor/bin/phpunit --coverage-text
Under 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.