Jump to content

random


chriscloyd

Recommended Posts

Okay im trying to do something that seems very hard that i cant even think of how to do it as of right now.  Okay.

I have 80 teams in a league i created im trying to run a script to make each team get matched up to 8 random teams

how could i go about doing this

Link to comment
Share on other sites

$team['1'] = "team 1";
$team['2'] = "team 2";
$how_many_teams = 8;
$i = "0";
foreach($team as $team2)
{
for($team3 = $team2; $i =0; $i >= $how_many_teams; $i++)
{
$rand_team = rand(1,80);
echo $team3." with team";
}
}

umm didn't really get what you meant but try this

Link to comment
Share on other sites

make an array containing all 80 teams, then do a foreach loop that will do a random 8 different times.

Everytime it finds a match, add it to a new array, check to make sure it isn't already in that array by using

in_array(). if its not append it to the end of the array. If it is in the array, make another search of the original

array. Keep doing this until you have run through the list 80 times.

Link to comment
Share on other sites

I believe the follow code does what you want:

<?php
$teams = range(1,80);
$selections = array();
for($j=0;$j<80;$j++) {
$tmp = array();
for($i=1;$i<9;$i++) {
	$tst = $teams[array_rand($teams)];
	while($tst != $teams[$j] && in_array($tst,$tmp)) $tst = $teams[array_rand($teams)];
	$tmp[$i] = $tst;
}
sort($tmp);
$selections[$j] = implode(',',$tmp);
}
echo '<pre>' . print_r($selections,true) . '</pre>';
?>

 

Ken

Link to comment
Share on other sites

<?php
//I didn't want to make any array... So i made it with a for loop.
//It uses numbers.
$array = array();
for($i = 1; $i < 81; $i++){
array_push($array, "$i");
}

foreach($array as $val){
$rand = array_rand($array, ;
echo $val. ' > '. $rand[0].'<br>';
echo $val. ' > '. $rand[1].'<br>';
echo $val. ' > '. $rand[2].'<br>';
echo $val. ' > '. $rand[3].'<br>';
echo $val. ' > '. $rand[4].'<br>';
echo $val. ' > '. $rand[5].'<br>';
echo $val. ' > '. $rand[6].'<br>';
echo $val. ' > '. $rand[7].'<br>';
}

?>

Example: http://tzfiles.com/testing/match.php

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.