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:
Did you know?Explore Trending and Topic pages for more stories like this.
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
43
Other forums
classic dynpro : hide area and auto adjust hights
Hi,
I want to develop a similar user interface as is used in standard transaction MIGO.
Error when call dll from oracle
Hi all, please help me!
I have dll write C language, I want call it from oracle procedur
Not "Just Another" MMORPG - text based.
Hello,
I've been learning MySQL and PHP for the last 2-3 years.
Though, I've had long paus
mod_rewrite.c on windows ??
why it's not working on windows while it's working on other hosts???
this is the code i got
default SAP userid
hi,, I just like to know if it is ok to use the default SAP user id (SAP*)?
How to ask a regex question?
Hi, I thought I'd share some of my insight after seeing too many bad questions that either get ignor
need help in dynamic select menu in php
hi i have created a dynamic select menu using php. i have a problem in that which is when the user s
Locking mysql tables with php
Hi
BACKGROUND:
I have multiple instances of the same php script running in a WAMP environ
how to get IP address?
hey guys,
i have this code which is supposed to get me the IP address of my site visitors:
mail() problem
Hi Guys,
I've set up a contact email form. It sends the information fine, but it sends it to