数学返回错误答案

时间:2013-12-31 07:57:05

标签: php math

我一直试图让这项工作工作近一个小时,而且它一直在躲避我。

public static function npcBattleStats($npc) {
    $npc = self::findNPC($npc);
    $rand = mt_rand(1.2, 3.3);
    $npcStats['attack'] = (($npc['level'] * $npc['power']) / $rand);
    $npcSpeeda = round($npc['power'] / 2.4);
    $npcSpeedb = round(($npc['power'] / 1.1) + 2);
    $npcStats['speed'] = mt_rand($npcSpeeda, $npcSpeedb);
    return $npcStats;
}

level = 3 | power = 50
$npcStats['attack'] = (($npc['level'] * $npc['power']) / $rand);

其他一切似乎都能正常运作,但攻击一直在关闭。应介于45和115之间,但仅返回以下内容。

[attack] => 50 | [attack] => 150 | [attack] => 75

我希望我在这里不会遗漏一些简单的内容,但非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

似乎Funktion mt_rand()只吐出aut整数,而不是浮点数。试试这个:

$rand = mt_rand(12, 33) / 10;

答案 1 :(得分:0)

首先,如cho所述 PHP手册:“返回值:min(或0)和max(或mt_getrandmax()之间的随机整数值),如果max小于min,则为FALSE。”

要解决这个问题,你需要这样的东西

$rand = mt_rand(35, 100) / 10;
//35 is your max
//120 is your low

所以当你得到: 你的最大值:

(5 * 50) / 3.5 = 42.85.. //so round it

你的低点:

(5 * 50) / 10 = 15