首页 >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