Last active
January 21, 2016 01:51
-
-
Save jacobsantos/32d85692d1a45afab743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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