Jump to content

pairs


chriscloyd

Recommended Posts

okay say i got 40 teams i wanna learn how to write the script where it randomly pairs two teams up to eachother 8 times but 8 dif teams not the same team as it paired up to b4 can someone help me? and then i want it to post into a table or file
Link to comment
Share on other sites

[!--quoteo(post=373420:date=May 13 2006, 12:13 AM:name=chriscloyd)--][div class=\'quotetop\']QUOTE(chriscloyd @ May 13 2006, 12:13 AM) [snapback]373420[/snapback][/div][div class=\'quotemain\'][!--quotec--]
okay say i got 40 teams i wanna learn how to write the script where it randomly pairs two teams up to eachother 8 times but 8 dif teams not the same team as it paired up to b4 can someone help me? and then i want it to post into a table or file
[/quote]

This will get you two random id's from a table:
[b]SELECT team_id FROM table1 WHERE ORDER BY RAND() LIMIT 2;[/b]

The rest of your question I don't understand. Try using proper centences.
Link to comment
Share on other sites

while i do understand your question, please note that we are not here to write your code for you. you need to make an effort to write your own code, and we will help you fix bugs....

HOWEVER... since I was extremely bored, I decided to do it anyways. Merry Christmas.

[code]
<?php

//generic array holding 40 values
$list = array('one','two','three','four','five',
              'six','seven','eight','nine','ten',
                            'eleven','twelve','thirteen','fourteen','fifteen',
                            'sixteen','seventeen','eighteen','nineteen','twenty',
                            'twentyone','twentytwo','twentythree','twentyfour','twentyfive',
                            'twentysix','twentyseven','twentyeight','twentynine','thirty',
                            'thirtyone','thirtytwo','thirtythree','thirtyfour','thirtyfive',
                            'thirtysix','thirtyseven','thirtyeight','thirtynine','fourty');

$max = 39; //starting amount of items (0-39 = 40)                            

// since you have 40 items you want to pair off, we will
// do the 'pair-off 20 times
for ($count = 0;$count <20; $count++) {
   //pick 2 random items from the list
     //and remove them from the list
     for ($number = 0; $number < 2; $number++) {
      $randompick = rand(0,$max);
        $numberpair[$number] = $list[$randompick];
        unset($list[$randompick]);
        $list = array_values($list);
        $max--;
   }
     //make a list of the items paired up
     $pairups[] = array('first' => $numberpair[0],
                        'second' => $numberpair[1]);
}

//generic printout of the list
$listofpairs = "<table border='1'><tr>";
$listofpairs.= "<td><b>First</b></td><td><b>Second</b></td></tr>";
for ($pairs = 0; $pairs < 20; $pairs++) {
   $listofpairs.="<tr>";
     foreach ($pairups[$pairs] as $val) {
      $listofpairs.="<td>" . $val . "</td>";
     }
     $listofpairs.="</tr>";
}
$listofpairs.="</table>";

echo $listofpairs;

?>
[/code]

if you have your items in a database then you would select them the way you normally would and replace the $list array with an array of the values pulled from the database.
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.