1

I am trying to figure out how to shuffle an array of URL's based on a percentage so that each URL get's picked a certain number of times.

<?php

  $urls = array(
  'http://www.google.com'=>'25%', 
  'http://www.yahoo.com'=>'25%',
  'http://www.bing.com' =>'50%');

I thought about going the rand() route and just getting a random number between 1-100 and making a switch statement with a bunch of ranges, but that seems less than elegant and clumsy. I also don't know how reliable doing this would be. I am trying to get as close to perfect as possible. If I shuffle through 100 times I am not sure most of the rand() examples would list the above array with google.com and yahoo.com getting selected 25 times each (+/- 2) and bing.com getting selected 50 times.

Is their a way to get accurate weighted shuffling? Thanks

1
  • Accurate as in Exactly 25-25-50, OR weighted random? (which is not 'Accurate')
    – Shad
    Aug 20, 2011 at 6:32

4 Answers 4

1

You'll still want a rand. In a distribution of 100, you're not likely to get a perfectly even 100% distribution. If you want that, you'll need to store state in a DB, or somehow static to the server itself. In fact, what you would do is just decrement the array values until they're all 0, then start again.

Here is a representation of how it would work without the perfect-match distribution (assuming it is in a function):

$urls = array(
  'http://www.google.com'=>25, 
  'http://www.yahoo.com'=>25,
  'http://www.bing.com' =>50);

// Assuming that totalWeight might not be 100 for some reason.
$totalWeight = array_sum(array_values($urls)); 

$currentWeight = 0;
$rand = rand(1,$totalWeight);

foreach ($urls as $key=>$value)
{
    $currentWeight += $value;
    if ($currentWeight > $rand)
    {
        return $key;
    }
}
1

How about the following logic?

  1. Order items so that higher-priority ones are at the top
  2. Get a random value from 1-100
  3. Now go through the list and substract the item's percentage from the random value until it becomes negative. When this happens stop iterating over the list and choose the item as the selected one
4
  • I mean put 'bing.com' =>'50%' as the first element in the list manually. Or use sort/usrot PHP functions to order it. Aug 20, 2011 at 6:43
  • I hear what you are saying. I just have no idea why you're saying it.
    – John Green
    Aug 20, 2011 at 6:44
  • Hm, yes, you're right - it doesn't matter how the items are ordered. Aug 20, 2011 at 6:46
  • S'ok, I've had plenty of those kinds of thoughts in my time. : ) I was really wondering if there was something I was missing.
    – John Green
    Aug 20, 2011 at 6:47
1

It looks like you could just do:

usort( $myArr, function( $a, $b ) {
   return str_replace( "%", "", $a )/rand(1,100) - 
          str_replace( "%", "", $b )/rand(1,100);
} );

To get the value in front, just call key:

key( $myArr );

Or, you could just iterate through the whole thing: the array is in a randomized value:

foreach( $myArr as $key => $val ) 
   // todo: do Something!
0

Try this

    <?php
    $urls = array(
    'http://www.google.com'=>25, 
    'http://www.yahoo.com'=>25,
    'http://www.bing.com' =>50);

    $count = 1;
    $random = rand(0, 100);

    foreach($urls as $url => $range) {
       if($random > ($range * ($count - 1)) && $random < ($range * ($count + 1)))
           return $url;
       $count++;
    }   
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.