Page 1 of 1

tie breaker code problem?

Posted: Mon Dec 13, 2004 3:58 pm
by deras
$sum is an array of 9 sums, i am trying to code things so that highest sum becomes $main, and if there is tie between two or more sums, then $main is selected randomly from the tying sums. the current code yields the highest scoring sum, but does not randomly break ties very well (if there is a tie). what do i need to add to break ties better? (currently the sum sooner or latter in the array gets selected when there is a tie, which means middle tieing numbers are never selected)

Code: Select all

<?php

$sum=split(":",$row[temp]);
$sumsort=$sum;
arsort($sumsort);
reset($sumsort);
foreach($sumsort as $k=>$v){
	$main=$k+1;break;

?>

Posted: Mon Dec 13, 2004 4:28 pm
by rehfeld
you mean
$sum is an array of 9 numbers?

could you explain a bit more, maybe give a sample?

Posted: Mon Dec 13, 2004 10:06 pm
by deras
yes that is what i meant

temp is a set of nine numbers like so.... 12:12:13:15:16:16:9:8:16

which becomes an array of nine numbers, $sum[0], $sum[1], etc


i am looking to pick out the highest number of those nine variables. and if there is a tie of one or more numbers randomly break the tie.

Posted: Mon Dec 13, 2004 10:20 pm
by John Cartwright

Code: Select all

<?php
$match = array();
$sum=split(":",$row['temp']);
$sumsort = sort($sum);
$count = count($sum);
foreach ($sumsort as $sumname => $sumvalue)
{
     if ($sumvalue == $sumsort[$count])
     {
          $match[] = $sumname;
     }
}
$newcount = count($match);
$rand = rand($newcount,$sumsort[$count]);
$newvalue = $sumsort[$rand];
echo $newvalue; // should return your winner
?>
hope this gives you an idea..........