-
<?php
-
/*********************************
-
RandomLib Version 1.0
-
Programmed by : Chao Xu(Mgccl)
-
E-mail :
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
-
Website : http://www.webdevlogs.com
-
Info : Please email me if there is any feature you want
-
or there is any bugs. I will fix them as soon as possible.
-
*********************************/
-
-
class random{
-
-
function add($string,$weight=1){
-
$this->
data[] =
array('s' =>
$string,
'w' =>
$weight);
-
}
-
function optimize(){
-
foreach($this->data as $var){
-
if($new[$var['s']]){
-
$new[$var['s']] += $var['w'];
-
}else{
-
$new[$var['s']] = $var['w'];
-
}
-
}
-
-
foreach($new as $key=>$var){
-
$this->
data[] =
array('s' =>
$key,
'w' =>
$var);
-
}
-
}
-
-
function select($amount=1){
-
if($amount == 1){
-
-
$result = $this->data[$rand]['s'];
-
}else{
-
$i = 0;
-
while($i<$amount){
-
$result[] =
$this->
data[array_rand($this->
data)]['s'];
-
++$i;
-
}
-
}
-
return $result;
-
}
-
-
function select_unique($amount=1){
-
if($amount == 1){
-
-
$result = $this->data[$rand]['s'];
-
}else{
-
-
foreach($rand as $var){
-
$result[] = $this->data[$var]['s'];
-
}
-
}
-
return $result;
-
}
-
-
function select_weighted($amount=1){
-
$count =
count($this->
data);
-
$i = 0;
-
$max = -1;
-
while($i < $count){
-
$max += $this->data[$i]['w'];
-
++$i;
-
}
-
if(1 == $amount){
-
-
$w = 0; $n = 0;
-
while($w <= $rand){
-
$w += $this->data[$n]['w'];
-
++$n;
-
}
-
$key = $this->data[$n-1]['s'];
-
}else{
-
$i = 0;
-
while($i<$amount){
-
-
++$i;
-
}
-
-
$i = 0;
-
$n = 0;
-
$w = 0;
-
while($i<$amount){
-
while($w<=$random[$i]){
-
$w += $this->data[$n]['w'];
-
++$n;
-
}
-
$key[] = $this->data[$n-1]['s'];
-
++$i;
-
}
-
}
-
return $key;
-
}
-
-
function select_weighted_unique($amount=1){
-
$count =
count($this->
data);
-
$i = 0;
-
if($amount >= $count){
-
while($i < $count){
-
$return[] = $this->data[$i]['s'];
-
++$i;
-
}
-
return $return;
-
}else{
-
$max = -1;
-
while($i < $count){
-
$max += $this->data[$i]['w'];
-
++$i;
-
}
-
-
$i = 0;
-
while($i < $amount){
-
$max -= $sub;
-
$w = 0;
-
$n = 0;
-
-
while($w <= $num){
-
$w += $this->data[$n]['w'];
-
++$n;
-
}
-
$sub = $this->data[$n-1]['w'];
-
$key[] = $this->data[$n-1]['s'];
-
-
unset($this->
data[$n-1]);
-
-
++$i;
-
}
-
return $key;
-
}
-
}
-
}
-
?>
-
-
-
Here are the example
-
-
<?php
-
-
$starttime =
$timeparts[1].
substr($timeparts[0],
1);
-
include ('class.random.php');
-
$random = new random;
-
$random->add('this have weight 1000, ', 1000);
-
$random->add('this have weight 1, ', 1);
-
$random->add('this have weight 200, ', 200);
-
$random->add('this have weight 500, ', 500);
-
?>
-
The random items:<br />
-
one weights 1000, one weights 1, one weights 200, one weights 500<br />
-
Result for randomly select 1 item:<?=$random->select()?><br />
-
Result for randomly select 3 item:
<? print_r($random->
select(3))?><br />
-
Result for randomly select 3 unique item:
<? print_r($random->
select_unique(3))?><br />
-
Result for weighted randomly select 1 item:<?=$random->select_weighted()?><br />
-
Result for weighted randomly select 3 item:
<? print_r($random->
select_weighted(3))?><br />
-
Result for weighted randomly select 3 unique item:
<? print_r($random->
select_weighted_unique(3))?><br />
-
<?php
-
-
$endtime =
$timeparts[1].
substr($timeparts[0],
1);
-
echo 'time spent',
bcsub($endtime,
$starttime,
6);
-
?>