Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Last active May 24, 2020 13:30
Show Gist options
  • Save PJZ9n/382c710d96ff3f42ed2b34eb4dc9ec34 to your computer and use it in GitHub Desktop.
Save PJZ9n/382c710d96ff3f42ed2b34eb4dc9ec34 to your computer and use it in GitHub Desktop.
確率計算
<?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