私は正常に動作するPHPの次のコードを持っています(実行するたびに多かれ少なかれ10の結果を返します):
function GetAboutTenRandomNumbers()
{
$result = array();
for ($i = 0; $i < 240; $i++)
{
if (Chance(10, 240) === true)
{
$result[] = $i;
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
return $result;
}
そして、Chance()関数は次のようになります。
function Chance($chance, $universe = 100)
{
$chance = abs(intval($chance));
$universe = abs(intval($universe));
if (mt_rand(1, $universe) <= $chance)
{
return true;
}
return false;
}
ここで、これらの10個の(平均)結果を次の4つのセグメントにランダムに分割します。
- 10%* 10=1の確率を持つ最初のもの
- 20%* 10=2の確率を持つ2番目のもの
- 30%* 10=3の確率を持つ3番目のもの
- 40%* 10=4の確率を持つ4番目のもの
ご覧のとおり、すべてのセグメントの合計(1 + 2 + 3 + 4)は10に等しいので、これを行うために次の関数をコーディングしました。
function GetAboutTenWeightedRandomNumbers()
{
$result = array();
// Chance * 10%
for ($i = 0; $i < 60; $i++)
{
if (Chance(10 * 0.1, 240) === true)
{
$result[] = $i;
}
}
// Chance * 20%
for ($i = 60; $i < 120; $i++)
{
if (Chance(10 * 0.2, 240) === true)
{
$result[] = $i;
}
}
// Chance * 30%
for ($i = 120; $i < 180; $i++)
{
if (Chance(10 * 0.3, 240) === true)
{
$result[] = $i;
}
}
// Chance * 40%
for ($i = 180; $i < 240; $i++)
{
if (Chance(10 * 0.4, 240) === true)
{
$result[] = $i;
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
return $result;
}
問題は、GetAboutTenWeightedRandomNumbers関数を数十回実行した結果が、GetAboutTenRandomNumbers関数によって返される結果よりもはるかに低いことです。私は根本的な数学の間違いを犯していると確信しています。どこにあるのか疑っていますが、それを解決する方法がわかりません。