Last active
August 29, 2015 14:24
-
-
Save khsk/bb8053a1645db10a3efc to your computer and use it in GitHub Desktop.
値をランダムに分配するにはどうすればいいのか ref: http://qiita.com/khsk/items/4dde8384832a405e6e5d
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
$target = array( | |
'A', | |
'B', | |
'C', | |
'D', | |
'E', | |
'F', | |
'G', | |
'H', | |
); | |
$target_cnt = count($target); | |
$total = 123456789; | |
echo $total . "\n"; | |
$result = array_fill(0, $target_cnt, 0); | |
$i = 0; | |
while ($total) { | |
if (!($i %= $target_cnt)) { | |
shuffle($result); | |
} | |
$rand = mt_rand(0, $total); | |
$result[$i] += $rand; | |
$total -= $rand; | |
$i++; | |
} | |
var_dump(array_combine($target, $result)); | |
echo $total . "\n"; | |
echo array_sum($result) . "\n"; | |
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
$target = array( | |
'A', | |
'B', | |
'C', | |
'D', | |
'E', | |
'F', | |
'G', | |
'H', | |
); | |
$target_cnt = count($target); | |
$total = 123456789; | |
echo $total . "\n"; | |
$result = array_fill(0, $target_cnt, 0); | |
$remainder = 0; // IDE警告消し | |
while ($total) { | |
$remainder = $total % $target_cnt; | |
$quotient = (int)($total / $target_cnt); | |
if ($quotient == 0) { | |
break; | |
} | |
for ($i = 0; $i < $target_cnt; $i++) { | |
$rand = mt_rand(0, $quotient); | |
$result[$i] += $rand; | |
$total -= $rand; | |
} | |
} | |
// 余りの分配 | |
$total -= $remainder; | |
$i = 0; | |
while ($remainder--) { | |
$i %= $target_cnt; | |
$result[$i++]++; | |
} | |
var_dump(array_combine($target, $result)); | |
echo $total . "\n"; | |
echo array_sum($result) . "\n"; | |
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
123456789 | |
array(8) { | |
["A"]=> | |
int(95557576) | |
["B"]=> | |
int(313427) | |
["C"]=> | |
int(2720550) | |
["D"]=> | |
int(448420) | |
["E"]=> | |
int(3296615) | |
["F"]=> | |
int(73736) | |
["G"]=> | |
int(172534) | |
["H"]=> | |
int(20873931) | |
} | |
0 | |
123456789 |
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
123456789 | |
array(8) { | |
["A"]=> | |
int(11087871) | |
["B"]=> | |
int(13496825) | |
["C"]=> | |
int(15902076) | |
["D"]=> | |
int(17920329) | |
["E"]=> | |
int(17281928) | |
["F"]=> | |
int(21869953) | |
["G"]=> | |
int(13350973) | |
["H"]=> | |
int(12546834) | |
} | |
0 | |
123456789 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment