Example #1
0
    //"складываем" элементы в массив в соответствие с их вероятностью
    $blackBox = array();
    foreach ($in as $key => $val) {
        $count = $val * $accuracy;
        for ($i = 0; $i < $count; $i++) {
            $blackBox[] = $key;
        }
    }
    //получаем случайный
    if ($count = count($blackBox)) {
        $k = rand(0, $count - 1);
        return $blackBox[$k];
    }
    return false;
}
//входные данные
$in = array('a' => 1 / 3, 'b' => 1 / 6, 'c' => 1 / 2);
$result = array();
//кол-во повторов
$repeat = 100;
//проверяем распределение на $repeat выборках
for ($i = 0; $i < $repeat; $i++) {
    $key = getRandomItem($in);
    if (!array_key_exists($key, $result)) {
        $result[$key] = 0;
    }
    $result[$key]++;
}
ksort($result);
echo "IN -> " . print_r($in, true);
echo "<br>OUT -> " . print_r($result, true);
Example #2
0
            }
            break;
        case "hyphens":
            if ($i < $wordCount - 1) {
                $nextWord = $nextWord . "-";
            }
            break;
        case "spaces":
            if ($i < $wordCount - 1) {
                $nextWord = $nextWord . " ";
            }
            break;
    }
    $result = $result . $nextWord;
    # Add a special symbol (in a random position), if requested.
    if ($incSpecial == "on" and $i == $specPosition) {
        $result = $result . getRandomItem($charList);
    }
    # Add a digit (in a random position), if requested.
    if ($incNumber == "on" and $i == $numPosition) {
        $result = $result . rand(0, 9);
    }
}
function getRandomItem($items)
{
    return $items[rand(0, count($items) - 1)];
}
function firstLetterToUpper($str)
{
    return substr_replace($str, strtoupper(substr($str, 0, 1)), 0, 1);
}