Zegg90

11-30-2006, 07:56 PM

Hi, I tried to write a weighted randomizing function, but I am having problems with its speed.

When I try to view the file, it seems like the function works fine, but it always reaches the maximum execution time... :(

Which basically rules it out from being used practically...

Here is the function I write, and some tests at the bottom.

If anybody could also point out some improvements, that would be great!

Thanks! :)

<?php

function item($item, $weight = array()) //$weight variable is optional

{

$pad = array_pad(array(), count($item), 1); //Change 1 to whatever you want the default weight to be

$weight = $weight+$pad;

ksort($weight); //Re-order the array

$weight2 = $weight; //Find smallest number of all weights

asort($weight2);

foreach($weight as $key=>$value)

{

$weight[$key] = round($value / $weight2[0]); //Divide all numbers to get smallest ratio with whole numbers

}

$total = 0;

foreach($weight as $w)

{

$total += $w; //Find total weight

}

$values_norm = array();

foreach($weight as $v) {

$values_norm[] = $v / $total; //Ratios...

}

$r = rand(0, 32767) / 32767; //Random number between 0 and 1

$n = 0;

//Check ratios and compare with the same random number

while ($r > $values_norm[$n]) { //Loop through each item

++$n;

}

$result = $item[$n];

return $result;

}

// Test 1

$array[] = 'test for speed of no weight arrays';

$array[] = 'how fast is this';

$array[] = 'the final result for this * 2';

echo "TEST 1:<br />";

echo item($array);

echo "<br /><hr /><br />";

//Test 2

$array2[] = 'test for weight';

$weight2[] = 30000;

$array2[] = 'with HUGE numbers';

$weight2[] = 2000;

$array2[] = 'final result for this * 5';

$weight2[] = 50000;

echo "TEST 2:<br />";

echo item($array2, $weight2);

echo "<br /><hr /><br />";

//Test 3

$array3[]= 'test for weight array only on some';

$weight3[] = 20000;

$array3[]= 'final result for this * 3';

echo "TEST 3:<br />";

echo item($array3, $weight3);

echo "<br /><hr /><br />";

?>

When I try to view the file, it seems like the function works fine, but it always reaches the maximum execution time... :(

Which basically rules it out from being used practically...

Here is the function I write, and some tests at the bottom.

If anybody could also point out some improvements, that would be great!

Thanks! :)

<?php

function item($item, $weight = array()) //$weight variable is optional

{

$pad = array_pad(array(), count($item), 1); //Change 1 to whatever you want the default weight to be

$weight = $weight+$pad;

ksort($weight); //Re-order the array

$weight2 = $weight; //Find smallest number of all weights

asort($weight2);

foreach($weight as $key=>$value)

{

$weight[$key] = round($value / $weight2[0]); //Divide all numbers to get smallest ratio with whole numbers

}

$total = 0;

foreach($weight as $w)

{

$total += $w; //Find total weight

}

$values_norm = array();

foreach($weight as $v) {

$values_norm[] = $v / $total; //Ratios...

}

$r = rand(0, 32767) / 32767; //Random number between 0 and 1

$n = 0;

//Check ratios and compare with the same random number

while ($r > $values_norm[$n]) { //Loop through each item

++$n;

}

$result = $item[$n];

return $result;

}

// Test 1

$array[] = 'test for speed of no weight arrays';

$array[] = 'how fast is this';

$array[] = 'the final result for this * 2';

echo "TEST 1:<br />";

echo item($array);

echo "<br /><hr /><br />";

//Test 2

$array2[] = 'test for weight';

$weight2[] = 30000;

$array2[] = 'with HUGE numbers';

$weight2[] = 2000;

$array2[] = 'final result for this * 5';

$weight2[] = 50000;

echo "TEST 2:<br />";

echo item($array2, $weight2);

echo "<br /><hr /><br />";

//Test 3

$array3[]= 'test for weight array only on some';

$weight3[] = 20000;

$array3[]= 'final result for this * 3';

echo "TEST 3:<br />";

echo item($array3, $weight3);

echo "<br /><hr /><br />";

?>