Hen*_*ler 2 php mysql math statistics
我正在制作纸牌游戏,我需要先创建卡片并将它们存储在mysql表格中.我正在试图弄清楚如何制作一个PHP脚本来创建它们.好吧,有6种类型的值从0到12不等,每张卡都有每个类别的值.每张卡中类别值的总和必须为36.所以每张卡都是这样的:
要么
甚至
我想如果我可以随机生成50或60张牌,那就太棒了.
提前致谢.
像这样的东西应该工作 - 逻辑可能有点偏,我还没有测试过.
function generate_card() {
$max_card = 12; // What's the highest value of a card number?
$min_card = 0; // What's the lowest value of a card number?
$num_cards = 6; // How many numbers on a card?
$total = 36; // What's the desired sum?
$numbers = array();
$running_total = 0;
for ($i = 0; $i < $num_cards; ++$i) {
$max = min($max_card, $total - $running_total); //The highest number we can choose
$min = max($min_card, $total - $running_total - ($max_card * ($num_cards - ($i + 1)))); //The lowest number we can choose
//- how much is left ? $total - $running total
//- min is amount_left - ($max_card
$number = rand($min, $max);
$running_total += $number;
$numbers[] = $number;
}
return $numbers;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我错放了一个括号和一个索引,它现在正常工作.