Last active
May 24, 2020 13:30
-
-
Save PJZ9n/382c710d96ff3f42ed2b34eb4dc9ec34 to your computer and use it in GitHub Desktop.
確率計算
This file contains hidden or 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 | |
class Calc | |
{ | |
/** @var int */ | |
private $all; | |
/** @var int[] */ | |
private $chance; | |
public function __construct(int $all = 65536) | |
{ | |
$this->all = $all; | |
} | |
public function addChance(int $id, int $chance): void | |
{ | |
$this->chance[$id] = $chance; | |
} | |
public function calcChance(): ?int | |
{ | |
$rand = mt_rand(1, $this->all); | |
$last = 1; | |
foreach ($this->chance as $id => $chance) { | |
$cl = $chance + $last; | |
if ($rand >= $last && $rand < $cl) { | |
return $id; | |
} | |
$last += $chance; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment