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

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

WBOY
WBOY原創
2016-06-06 19:39:301364瀏覽

无详细内容 $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;
}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn