当前位置 : 主页 > 网络编程 > PHP >

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

来源:互联网 收集:自由互联 发布时间:2021-06-30
$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 = arra
 
$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;
}

网友评论