chriscloyd Posted March 26, 2007 Share Posted March 26, 2007 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 https://forums.phpfreaks.com/topic/44386-random/ Share on other sites More sharing options...
desithugg Posted March 26, 2007 Share Posted March 26, 2007 $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 https://forums.phpfreaks.com/topic/44386-random/#findComment-215553 Share on other sites More sharing options...
The Little Guy Posted March 26, 2007 Share Posted March 26, 2007 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 https://forums.phpfreaks.com/topic/44386-random/#findComment-215627 Share on other sites More sharing options...
kenrbnsn Posted March 26, 2007 Share Posted March 26, 2007 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 https://forums.phpfreaks.com/topic/44386-random/#findComment-215630 Share on other sites More sharing options...
The Little Guy Posted March 26, 2007 Share Posted March 26, 2007 <?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 https://forums.phpfreaks.com/topic/44386-random/#findComment-215632 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.