I am trying to take the values in an array and randomly assign them to one another. An example of what I am talking about would be if you wanted to have a list of students who will be randomly paired as presenter and evaluator.
The array holds the names like:
0 = Bobby
1 = Tim
2 = Sally
3 = Fred
I want the array to randomly pair the students, something like this:
Bobby with Tim
Tim with Sally
Sally with Fred
Fred with Bobby
So that each student is, in one pairing the presenter, and in another pairing the evaluator. They do not necessarily have to be different for each match (ie. I could have Bobby with Tim and Tim with Bobby) but I do want to avoid Bobby with Bobby, and also need to avoid doubling up presenters or evaluators like: Bobby with Sally and Tim with Sally.
I wrote some code that assigns the values into two identical arrays. Then it uses the shuffle($array) function on one to randomize the order. Then I put those through a loop that tests to verify that array1[0] != array2[0] to prevent someone from being matched to themselves, if they are, it resets the loop, performs the shuffle function again, and starts testing again.
This works perfectly when working with 4 or 5 students, but times out with only 10. I know there has got to be some built in function or something I am missing. I need this to be able to work for large classes of 25 to 50.
Any ideas would be much appreciated.
Travis
Random Values in Array Problem - HELP!
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Have you looked at the array functions for PHP?
Everah | Please use
Everah | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I figured it out... the time out was due to a stupid mistake in the code, not the code itself.
Here's what worked:Code: Select all
$match = $person; # assign submitted names into an idetical array.
#shuffle the new array
$i = 0;
while($person[$i]) {
if($i==0) {
shuffle($match);
}
if($person[$i]==$match[$i]) { #test to see if there are any duplicates, if so reset the while loop
$i=0;
}
else {
$i ++;
}
}
$i = 0;
while($person[$i]) { # echo out the matches - This is set in a second loop to prevent the echoing or failed attempts in the last loop.
echo "$person[$i] matched with $match[$i]<P>";
$i ++;
}Everah | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
I had a go at it myself, my code is suprisingly similar to yours.
I think it's a very poor way of doing it, I've crashed my server twice now refreshing the script, lol. They'll be an easier way which someone will point out.
Here's my attempt for you:
I think it's a very poor way of doing it, I've crashed my server twice now refreshing the script, lol. They'll be an easier way which someone will point out.
Here's my attempt for you:
Code: Select all
<?php
$students =
array(
'John',
'Ray',
'Pete',
'Jeff',
'Richard'
);
$shuffled = $students;
shuffle($shuffled);
$x = 0;
$matches = array();
while(count($matches) < 5) {
if($students[$x] == $shuffled[0] || in_array($shuffled[0], $matches)) {
shuffle($shuffled);
} else {
$matches[$students[$x]] = $shuffled[0];
$x++;
}
}
foreach($matches as $k => $v) echo $k.' with '.$v.'<br />';
?>