Shuffle Array


Posted on 16th Feb 2014 07:03 pm by admin

Hi,

I am writing a script for a game that needs players to randomly be assigned a target (another player) in a loop so that each player is matched with someone other than who has them. See below:

Andy matched with Jan
Jan matched with Alex
Alex matched with Sue
Sue matched with Andy

My code retrieves name from the database in the order they sign up to play. I need to randomize the pairings for fairness.

Code: <?
$targets = array("Andy","Jan","Alex","Sue"); //names from DB in order of signup
?>
I make a copy of the array, shuffle it and check to make sure that two players are not paired with each other.

Code: <?
$targets = array("Andy","Jan","Alex","Sue"); //names from DB in order of signup
$targets_shuffled = $targets; //copy array
shuffle($targets_shuffled); //shuffle targets
?>
I am having trouble figuring out how to check for duplicate pairings and then reshuffling until it works. Originally I had looped through the arrays to check each pairing:
Code: <?
if($targets[$i] == $targets_shuffled[$i]){
echo "same!";
}
?>
Then I realized when one pair was matched incorrectly the whole pairing would need to be reshuffled. I tried to flag it, break out, and reshuffle, but could never get anything that worked.

Any ideas?
-Blake

Code currently:
Code: <?
shuffle($targets_shuffled);
echo "<br /><br />";

$retry = true;
$tries = 1;

while($retry == true){
echo "try: $tries<br />";
$retry = false;
$i = 0;
while($i < count($targets)){
if($targets[$i] == $targets_shuffled[$i]){
echo "found match!<br />";
$retry = true;
echo "breaking<br />";
break;
}
$i++;
}
$tries++;
}

?>

No comments posted yet

Your Answer:

Login to answer
199 Like 43 Dislike
Previous forums Next forums
Other forums

InternetOpenUrl() Invalid cert
Hi all,

Does anyone know how to prevent calls to InternetOpenUrl() from failing with erro

Display a default image
I am trying to display generic image for items that don't have one, but I can't get it to display.

Issues: PHP Forms -Clearing
I've tried looking online before actually asking for help, but I've been looking for about the past

Comma seperated implode & modifying returned string.
I have a PHP post form that posts up to 4 variables. I need whatever variables are passed to be comb

Help on Order Entry Form/System Where is best to begin.
I have a dilema and a very short amount of time at this point and I'm looking for some help on decid

Anti Spam Code Problems
Ok where do i start? Probably by telling you I have very little working knowledge of PHP and that I

Motivational Posters
Hello,

I wonder how can i do them in php. Are there any sample codes that you can show me?

array ...
hi
Code: function formatCategories($categories, $parentId)
{
// $navCat stores all child

disabling a button server-side then re-enabling client-side breaks button postback
I have a tabbed container and a button (not in the container) on a page. If the first tab is selecte

Email Form Syntax Issue
I need the TO: in email to display To: CEO instead of To: abc@mail.com

How to alter the scri

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash