jpuck/peer-value-distributer

Given an array, return a new array with the same keys and values randomly distributed evenly amongst the peers in the specified batch quantity.

1.0.1 2017-01-28 22:35 UTC

This package is not auto-updated.

Last update: 2024-04-09 21:46:26 UTC


README

Given an array, return a new array with the same keys and values distributed randomly and evenly amongst the peers in the specified batch quantity.

Build Status Codecov

For example, say you have a group of 15 book authors. You want each author to review 3 of their peers' work. This function will randomly assign each author 3 of its peers to read.

  • Every book is reviewed exactly 3 times.
  • No one will review any book more than once.
  • No one will review their own book.

The natural maximum distribution count is n-1 so if you specify a count larger than that, it will be ignored and the largest possible count will be used instead.

e.g. if you distribute those 15 books with a count of 20, it will be ignored and instead every author will be assigned the maximum of 14 peer books to review.

Registered on packagist for easy installation using composer.

composer require jpuck/peer-value-distributer

Example

use jpuck\PeerValueDistributer;

$books = [
    'John' => 'The Book of John',
    'Jane' => 'A Work by Jane',
    'Jeff' => 'Readings from Jeff',
    'Jose' => 'Words of Jose',
    'Jena' => 'Writing with Jena',
];

print_r( PeerValueDistributer::distribute($books, $count = 3) );

Output:

Array
(
    [Jena] => Array
        (
            [John] => The Book of John
            [Jeff] => Readings from Jeff
            [Jane] => A Work by Jane
        )

    [John] => Array
        (
            [Jeff] => Readings from Jeff
            [Jane] => A Work by Jane
            [Jose] => Words of Jose
        )

    [Jeff] => Array
        (
            [Jane] => A Work by Jane
            [Jose] => Words of Jose
            [Jena] => Writing with Jena
        )

    [Jane] => Array
        (
            [Jose] => Words of Jose
            [Jena] => Writing with Jena
            [John] => The Book of John
        )

    [Jose] => Array
        (
            [Jena] => Writing with Jena
            [John] => The Book of John
            [Jeff] => Readings from Jeff
        )

)