Skip to content

Instantly share code, notes, and snippets.

@jacobsantos
Last active January 21, 2016 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsantos/32d85692d1a45afab743 to your computer and use it in GitHub Desktop.
Save jacobsantos/32d85692d1a45afab743 to your computer and use it in GitHub Desktop.
<?php
$data = [1,1,1,1,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4];
$chunks = array_count_values($data);
$values = array_keys($chunks);
$used = array_combine($values, array_fill(0, count($values), 0));
$new_distribution = [];
$previous = null;
for ($i=count($data); $i > 0; $i--) {
$iterations = 0; // Prevent infinite loop
do {
$value = $values[mt_rand(0, count($values)-1)];
} while($previous === $value && $iterations++ < 11);
if ($used[$value] >= $chunks[$value]) {
unset($values[array_search($value, $values)]);
$values = array_values($values);
}
$new_distribution[] = $value;
$used[$value]++;
$previous = $value;
}
$avg_distribution = array_slice($new_distribution, 0, floor(count($data) / array_keys($chunks)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment