Leaditin\Distribution
A simple PHP API for distributing values based on their probabilities, (*1)
![Build Status][ico-build]
, (*2)
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:, (*3)
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., (*4)
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., (*5)
This is where Leaditin\Distribution will help you:, (*6)
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., (*7)