By endroid
This library extends the basic random functionality by ensuring that the appearance of items is evenly distributed while respecting their priority. Items with priority 3 will be shown three times as often as items with priority 1, while the shuffle algorithm ensures that the values are spreaded nicely.
Use Composer to install the library.
$ composer require endroid/priority-shuffle-random<?php
use Endroid\PriorityShuffleRandom;
$random = new PriorityShuffleRandom();
$random->add('A', 1);
$random->add('B', 2); // Show B two times as often as the other items
$random->add('C', 1);
$random->add('D', 1);
for ($i = 0; $i < 12; $i++) {
echo $random->next();
}
// Example output: CABDBDBACBACVersion numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibility breaking changes will be kept to a minimum but be aware that these can occur. Lock your dependencies for production and test your code when upgrading.
This bundle is under the MIT license. For the full copyright and license information please view the LICENSE file that was distributed with this source code.