tie breaker code problem?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

tie breaker code problem?

Post 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;

?>
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

you mean
$sum is an array of 9 numbers?

could you explain a bit more, maybe give a sample?
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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..........
Post Reply