Home >php教程 >php手册 >基于时间随机返回比率的随机值

基于时间随机返回比率的随机值

WBOY
WBOYOriginal
2016-06-06 19:39:301369browse

无详细内容 $config = array( 'sohu' = 10, 'sina' = 30, 'qq' = 20, 'baidu' = 20,);echo randomBaseTimestamp($config);function randomBaseTimestamp($data = array()) { uasort($data, function ($a, $b) { return rand(-1, 1); }); $time = time(); $to

$config = array(
    'sohu' => 10,
    'sina' => 30,
    'qq' => 20,
    'baidu' => 20,
);

echo randomBaseTimestamp($config);

function randomBaseTimestamp($data = array()) {
    uasort($data, function ($a, $b) {
        return rand(-1, 1);
    });
    $time = time();
    $total = array_sum($data);
    $mod = $time%$total;
    $result = '';
    foreach($data as $k => $v) {
        $result = $k;
        $mod -= $v;
        if($mod<=0) {
            break;
        }
    }

    return $result;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn