How to do random with priority? [duplicate]

2020-07-23 04:38发布

This is my code, for example:

<?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),
     );
?>

I want the system to randomly display one of the url's on each refresh. I want it to display the higher priority more times than the lower. I need it for banner system, and the ones with higher priority paying more, so they should be seen more.

How to do it?

标签: php random
6条回答
聊天终结者
2楼-- · 2020-07-23 04:58

Add new field into array, named "prob" that contains the probability of element to show.

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

After that show the item basing on its priority:

$p = rand(1, $prob);
for ($i=count($array)-1; $i>=0; $i--)
  if ($arr[$i]["prob"] <= $p) {
    // Show this item
    // ...
    break;
  }
查看更多
smile是对你的礼貌
3楼-- · 2020-07-23 04:58
$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)];
查看更多
叛逆
4楼-- · 2020-07-23 04:59
$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
}
查看更多
冷血范
5楼-- · 2020-07-23 05:14

There is a working example : 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),
 );

// Recreate another array where we have multiple occurence of the same value (nb_of_occurence = priority)

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

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

// Count the number of elements in this new array

$nbOfElement = count($listOfUrl);

// Generate a random index between 0 and (number of element - 1)

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

// Retrive the random value

$randomURL = $listOfUrl[$randomIndex];

echo $randomURL;
查看更多
Rolldiameter
6楼-- · 2020-07-23 05:16

Iterate over all the banners, and assign a key (numeral id) for each. To those with a higher priority, assign 2 keys (or more if you wish an even higher priority). Then just find the random number between 0 (assuming it's zero-based) and the total number of keys:

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

UPDATE

Here is some code:

// $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;      
     }
}

Then, to fetch a random url, but with priorities, do this:

$arr[ $keys[ rand(0, count($keys) - 1) ] ];
查看更多
Deceive 欺骗
7楼-- · 2020-07-23 05:17

You can add items to an array based on their priority. If an item has a priority of 2, you can add it to the array twice. Then you can pull out a random item from the array.

// 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;
}
查看更多
登录 后发表回答