Hallo Community,
irgendwie werde ich aus diesem Script nicht schlau:
$probabilities enthält zwei Werte:
PHP-Code:
$pInjured[True] = 30
$pInjured[FALSE] = 70
Die Nachfolgende Funktion soll wohl eine der beiden Werte auswählen: Denn die Funktion wird wie folgt aufgerufen:
PHP-Code:
$injured = SimulationHelper::selectItemFromProbabilities($pInjured);
PHP-Code:
/**
* Picks an item from the specified set (array). The selection is picked randomly, but considering the specified probability.
*
* @param array $probabilities assoc. array with key=value to be returned if item is selected, value=probability (0-100).
* @return mixed Selected item picked from the specified set.
*/
public static function selectItemFromProbabilities($probabilities) {
$magicNo = self::getMagicNumber();
$oldBoundary = 0;
foreach ($probabilities as $key => $probability) {
$newBounday = $oldBoundary + $probability;
if ($magicNo > $oldBoundary && $magicNo <= $newBounday) {
return $key;
}
$oldBoundary = $newBounday;
}
// return last element, since probabilities are not 100 per cent
return end($probabilities);
Nach meinem Verständnis
wird für jeden übergebenen Wert:
PHP-Code:
$newBounday = $oldBoundary + $probability; generiert:
also 70 und 30
dann wird der Wert mit der MagicNumber verglichen (z.B. 20)
PHP-Code:
if ($magicNo > $oldBoundary && $magicNo <= $newBounday) {
return $key;
20 > 0 und 20 <= 30 = $key
20 > 0 und 20 <= 70 = $key
Beide Bedingungen sind ja erfült.
In der Kommentierung steht dann:
Code:
// return last element, since probabilities are not 100 per cent
Demzufolge müsste $pInjured[FALSE] = 70 übergeben werden oder sehe ich das falsch?
Lesezeichen