如何优先随机做?

时间:2014-03-11 19:11:58

标签: php random

这是我的代码,例如:

<?php
     $arr = array(
          array("url" => "http://google.com", "priority" => 2),
          array("url" => "http://facebook.com", "priority" => 2),
          array("url" => "http://youtube.com", "priority" => 2),
          array("url" => "http://stackoverflow.com", "priority" => 1),
          array("url" => "http://kickass.to", "priority" => 1),
          array("url" => "http://twitter.com", "priority" => 1),
          array("url" => "http://example.com", "priority" => 1),
     );
?>

我希望系统在每次刷新时随机显示其中一个网址。我希望它显示的优先级高于优先级。我需要它用于横幅系统,而那些优先级更高的人支付更多,所以他们应该被更多地看到。

怎么做?

6 个答案:

答案 0 :(得分:3)

您可以根据项目的优先级向项目添加项目。如果项目的优先级为2,则可以将其添加到阵列两次。然后你可以从数组中提取一个随机项。

// CREATE A NEW ARRAY TO HOLD ALL OF THE BANNERS
$banner_array = array();     

// LOOP THROUGH EACH ITEM IN THE ARRAY CURRENTLY HOLDING THE BANNERS
foreach ($arr AS $banner) {

    // FOR EACH NUMBER IN THE PRIORITY, ADD THE ITEM TO OUR NEW ARRAY
    // google.com, facebook.com, youtube.com WILL BE ADDED IN TWICE
    for ($i = 0; $i < $banner['priority']; $i++) {
        $banner_array[] = $banner['url'];
    }
}

// COUNT THE TOTAL NUMBER OF ITEMS IN OUR ARRAY
// WE WILL PICK OUT A NUMBER BETWEEN ZERO AND THIS NUMBER (MINUS 1)
$item_count = count($banner_array) - 1;

// ONCE WE HAVE A RANDOM NUMBER, WE CAN ACCESS THAT ITEM OF THE ARRAY
print "RANDOM URL: ".$banner_array[get_random_item($item_count)];


// THIS FUNCTION PICKS A NUMBER BETWEEN ZERO AND THE NUMBER OF ITEMS IN OUR ARRAY
function get_random_item($item_count) {
    mt_srand(microtime() * 1000000);
    $random_number = rand(0, $item_count);
    return $random_number;
}

答案 1 :(得分:1)

迭代所有横幅,并为每个横幅分配一个键(数字ID)。对于具有更高优先级的用户,请分配2个键(如果您希望具有更高的优先级,则分配更多键)。然后找到0之间的随机数(假设它是从零开始)和键的总数:

rand(0, count($keys) - 1);

<强>更新

以下是一些代码:

// $arr = Your original array

$keys = array();
for ($i = 0; $i < count($arr); $i++) { // you can also use foreach here
     for ($u = 0; $u < $arr[$i]['priority']; $u++) {
         $keys[] = $i;      
     }
}

然后,要获取随机网址,但是有优先级,请执行以下操作:

$arr[ $keys[ rand(0, count($keys) - 1) ] ];

答案 2 :(得分:0)

$prob = rand(0,9);
if($prob<2){
    //show google url
}else if($prob<4){
    //show facebook url
}else if($prob<6){
    //show youtube url
}else if($prob<7){
    //show stackoverflow url
}else if($prob<8){
    //show kickass url
}else if($prob<9){
    //show twitter url
}else{
    //show example url
}

答案 3 :(得分:0)

将新字段添加到数组中,命名为&#34; prob&#34;包含要显示元素的概率。

$prob = 0;
foreach($arr as $idx => $val) {
  $prob += $arr[$idx]["priority"];
  $arr[$idx]["prob"] = $prob; 
}

之后显示基于其优先级的项目:

$p = rand(1, $prob);
for ($i=count($array)-1; $i>=0; $i--)
  if ($arr[$i]["prob"] <= $p) {
    // Show this item
    // ...
    break;
  }

答案 4 :(得分:0)

有一个有效的例子:http://3v4l.org/u5WNS

$arr = array(
      array("url" => "http://google.com", "priority" => 2),
      array("url" => "http://facebook.com", "priority" => 2),
      array("url" => "http://youtube.com", "priority" => 2),
      array("url" => "http://stackoverflow.com", "priority" => 1),
      array("url" => "http://kickass.to", "priority" => 1),
      array("url" => "http://twitter.com", "priority" => 1),
      array("url" => "http://example.com", "priority" => 1),
 );

//重新创建另一个我们有多个相同值的数组(nb_of_occurence = priority)

$listOfUrl = array();
foreach ($arr as $url) {
    $nbOfOccurence = $url['priority'];

    for($i = 0 ; $i < $nbOfOccurence ; $i++) {
        $listOfUrl[] = $url['url'];
    }
} 

//计算此新数组中元素的数量

$nbOfElement = count($listOfUrl);

//生成0和(元素数量-1)之间的随机索引

$randomIndex = rand(0,($nbOfElement - 1));

//重复随机值

$randomURL = $listOfUrl[$randomIndex];

echo $randomURL;

答案 5 :(得分:0)

$prob_arr = array();
$count=0;
foreach($arr as $key=>$val){
    for($j=0;$j<$val;$j++){
        $prob_arr[$i] = $key;
        $count++;
    }
}

$banner = prob_arr[rand(0,$count-1)];