Jump to content

Random Assignment


craygo

Recommended Posts

I am trying to create a php application for my golf league. What I need to do is setup matches. Here is a senario:

I have 20 players
we play for 20 weeks
All players are in a MySql database with a unique ID for each
What I want to do is have php randomize matches for the players for all 20 weeks but not play the same person if at all possible. Obviously if it were 10 players for 20 weeks then playing someone twice would be fine but not till he has played everyone first.

Thanks

Ray
Link to comment
Share on other sites

basically...

Select all players from the golf_league table, and put them in order by their names.
Then, for each golfer, select all players from the golf_league in random order.

If the golfer they're playing against has the same name as them, leave them off the list, else display the name of their opponent and the week number.

[code]
<?php


$strSQL = "SELECT * FROM golf_league ORDER BY golfers_name ASC";
if ($result = mysql_query($strSQL) or die ("Invalid query"))
{
    $num = mysql_num_rows($result);
    for($i=0;$i<$num;$i++)
    {
        $golfers_name = mysql_result($result,$i,"golfers_name");

    echo "$golfers_name will play against:<br>";

    $strSQL2 = "SELECT * FROM golf_league ORDER BY RAND()";
    if ($result2 = mysql_query($strSQL2) or die ("Invalid query"))
    {
        $num2 = mysql_num_rows($result2);

        $week = 1;
        for($i2=0;$i2<$num2;$i2++)
        {    
            $golfers_name2 = mysql_result($result,$i,"golfers_name");
        
        if($golfers_name2!=="$golfers_name"){
        echo "Week $week. $golfers_name<br>";}
        $week++;
        }
    }
    }
}
?>
[/code]
Link to comment
Share on other sites

ok I got the idea you have there, but I still got a problem. With what you have the randow number is picked but it can also be repeated. So I am coming up with things like this.

My players all have unique ids in order so I only have to use the database to get the number of players. I have this code based a little on wisewood's post.

[code]<?php
// will get this number from database
$players = '20';
// add one to the weeks because I want to start at 1 and go the desired weeks
$weeks = 10+1;
// loop through the desired weeks
for($i=1;$i<$weeks;$i++){
$p1 = rand((($players/2)+1), $players);
  if($i !== $p1){
  echo "$i vs $p1<br>";
  }
}
?>[/code]

Which returns
[code]1 vs 14
2 vs 18
3 vs 14
4 vs 14
5 vs 14
6 vs 13
7 vs 11
8 vs 19
9 vs 20
10 vs 13[/code]

Obviously player 14 cannot play 4 people in one week

I want it to look like this but without all the other stuff. I just need the 1 vs 2 part
[a href=\"http://eagolf.theelders.net/2006/schedule.pdf\" target=\"_blank\"]http://eagolf.theelders.net/2006/schedule.pdf[/a]

Any thoughts??

Ray
Link to comment
Share on other sites

Put the ones it has come up with in an array. If it comes up with that same number again, redo your randomizing before moving on to the next match. You can use in_array() to see if the new random number is in your "used" array.
Link to comment
Share on other sites

will it be a problem to have both numbers in the array. Reason I am asking is I eventually need to insert these into a table, so I want to have it in a format I can insert.

I think this will be tough to do, because eventually I may use this elsewhere and if there is a case where I only have 10 teams but 20 weeks of golf, then the teams will play each other more than once. So the no repeat will have to cancel out itself once all teams have been played. I am not even sure this is posible with php but I am trying like hell to do it.

If you want table structure I have right now i will post

Ray
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.