We have 17 guests online
   
PHP Scripts arrow Free PHP Tutorials arrow Free PHP Tutorials arrow PHP Tutorials and Examples arrow PHP 
Free PHP Tutorials arrow PHP Tutorials and Examples arrow PHP

RandomLib
User Rating: / 0
PoorBest 

This class can be used to pick a random item from a collection.

It can add to a collection of items that can be values of any type: strings, numbers, objects, etc.. Each item may have a probability weight.

The class can pick one or more random items from the collection. When picking multiple items it may allow repetition of the same item or assure that all the picked items are unique.

It can also pick random items considering their probability weight, so those with greater weight are more likely to be picked.

  1. <?php
  2. /*********************************
  3. RandomLib Version 1.0
  4. Programmed by : Chao Xu(Mgccl)
  5. E-mail        : This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
  6. Website       : http://www.webdevlogs.com
  7. Info          : Please email me if there is any feature you want
  8. or there is any bugs. I will fix them as soon as possible.
  9. *********************************/
  10.    
  11. class random{
  12. var $data = array();
  13. function add($string,$weight=1){
  14.     $this->data[] = array('s' => $string, 'w' => $weight);
  15. }
  16. function optimize(){
  17.     foreach($this->data as $var){
  18.         if($new[$var['s']]){
  19.             $new[$var['s']] += $var['w'];
  20.         }else{
  21.             $new[$var['s']] = $var['w'];
  22.         }
  23.     }
  24.     unset($this->data);
  25.     foreach($new as $key=>$var){
  26.         $this->data[] = array('s' => $key, 'w' => $var);
  27.     }
  28. }
  29.  
  30. function select($amount=1){
  31.     if($amount == 1){
  32.         $rand = array_rand($this->data);
  33.         $result = $this->data[$rand]['s'];
  34.     }else{
  35.         $i = 0;
  36.         while($i<$amount){
  37.             $result[] = $this->data[array_rand($this->data)]['s'];
  38.             ++$i;
  39.         }
  40.     }
  41.     return $result;
  42. }
  43.  
  44. function select_unique($amount=1){
  45.     if($amount == 1){
  46.         $rand = array_rand($this->data);
  47.         $result = $this->data[$rand]['s'];
  48.     }else{
  49.         $rand = array_rand($this->data, $amount);
  50.         foreach($rand as $var){
  51.             $result[] = $this->data[$var]['s'];
  52.         }
  53.     }
  54.     return $result;
  55. }
  56.  
  57. function select_weighted($amount=1){
  58.     $count = count($this->data);
  59.     $i = 0;
  60.     $max = -1;
  61.     while($i < $count){
  62.         $max += $this->data[$i]['w'];
  63.         ++$i;
  64.     }
  65.     if(1 == $amount){
  66.         $rand = mt_rand(0, $max);
  67.         $w = 0; $n = 0;
  68.         while($w <= $rand){
  69.             $w += $this->data[$n]['w'];
  70.             ++$n;
  71.         }
  72.         $key = $this->data[$n-1]['s'];
  73.     }else{
  74.         $i = 0;
  75.         while($i<$amount){
  76.             $random[] = mt_rand(0, $max);
  77.             ++$i;
  78.         }
  79.         sort($random);
  80.         $i = 0;
  81.         $n = 0;
  82.         $w = 0;
  83.         while($i<$amount){
  84.             while($w<=$random[$i]){
  85.                 $w += $this->data[$n]['w'];
  86.                 ++$n;
  87.             }
  88.             $key[] = $this->data[$n-1]['s'];
  89.             ++$i;
  90.         }
  91.     }
  92.     return $key;
  93. }
  94.  
  95. function select_weighted_unique($amount=1){
  96.     $count = count($this->data);
  97.     $i = 0;
  98.     if($amount >= $count){
  99.         while($i < $count){
  100.             $return[] = $this->data[$i]['s'];
  101.             ++$i;
  102.         }
  103.         return $return;
  104.     }else{
  105.         $max = -1;
  106.         while($i < $count){
  107.             $max += $this->data[$i]['w'];
  108.             ++$i;
  109.         }
  110.        
  111.         $i = 0;
  112.         while($i < $amount){
  113.             $max -= $sub;
  114.             $w = 0;
  115.             $n = 0;
  116.             $num = mt_rand(0,$max);
  117.             while($w <= $num){
  118.                 $w += $this->data[$n]['w'];
  119.                 ++$n;
  120.             }
  121.             $sub = $this->data[$n-1]['w'];
  122.             $key[] = $this->data[$n-1]['s'];
  123.            
  124.             unset($this->data[$n-1]);
  125.             $this->data = array_merge($this->data);
  126.             ++$i;
  127.         }
  128.         return $key;
  129.     }
  130. }
  131. }
  132. ?>
  133.  
  134.  
  135. Here are the example
  136.  
  137. <?php
  138.   $timeparts = explode(' ',microtime());
  139.   $starttime = $timeparts[1].substr($timeparts[0],1);
  140.     include ('class.random.php');
  141.     $random = new random;
  142.     $random->add('this have weight 1000, ', 1000);
  143.     $random->add('this have weight 1, ', 1);
  144.     $random->add('this have weight 200, ', 200);
  145.     $random->add('this have weight 500, ', 500);
  146. ?>
  147. The random items:<br />
  148. one weights 1000, one weights 1, one weights 200, one weights 500<br />
  149. Result for randomly select 1 item:<?=$random->select()?><br />
  150. Result for randomly select 3 item:<? print_r($random->select(3))?><br />
  151. Result for randomly select 3 unique item:<? print_r($random->select_unique(3))?><br />
  152. Result for weighted randomly select 1 item:<?=$random->select_weighted()?><br />
  153. Result for weighted randomly select 3 item:<? print_r($random->select_weighted(3))?><br />
  154. Result for weighted randomly select 3 unique item:<? print_r($random->select_weighted_unique(3))?><br />
  155. <?php
  156.       $timeparts = explode(' ',microtime());
  157.   $endtime = $timeparts[1].substr($timeparts[0],1);
  158.   echo 'time spent',bcsub($endtime,$starttime,6);
  159. ?>

 

 

Directory Stats

There are 708 listing and 43 categories in our website

Change Language

The eBay Song
Advertising
Remortgages
Loans
Online Dating