leaditin / distribution
Probability distribution service
Requires
- php: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ^5.7 || ^6.0
This package is auto-updated.
Last update: 2024-11-04 18:09:09 UTC
README
A simple PHP API for distributing values based on their probabilities
Installation
The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json
:
composer require leaditin/distribution
Usage
Imagine that you want to simulate creation of 100 users where each must have defined gender. You want to have 53% female and 47% male.
Imagine that you do not want to generate all female users and after that all male users, instead of that you want these records to be generated randomly.
This is where Leaditin\Distribution
will help you:
use Leaditin\Distribution\Collection; use Leaditin\Distribution\Distributor; use Leaditin\Distribution\Element; use Leaditin\Distribution\Exception\DistributorException; $probabilities = new Collection( new Element('MALE', 53), new Element('FEMALE', 47) ); $distributor = new Distributor($probabilities, 100); # Create user with random gender $user = new \User(); $user->gender = $distributor->useRandomCode(); $user->save(); # Create user with explicit gender $user = new \User(); $user->firstName = 'Jon'; $user->lastName = 'Snow'; $user->gender = $distributor->useCode('MALE'); $user->save();
Credits
License
Released under MIT License - see the License File for details.