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++;
}

?>

199 Like 43 Dislike
Previous php-forum Next php-forum
Other php-forum

problem with refreshing
in my site, i have an index page, it has a main div. this main div's content is changing according t

rand() function
just a general question guys a girls, is the rand() function 100% random or is it based on time?

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.

Strange php code found on my website
I got hacked and the following code was placed within my files:

Code: [Select]<?
/*

Hom to make one url to open together with another url
I have a chat, which i want to be opened, as soon as the users login to the site. As it is now, when

Web Application Recipe
Hi Guys!

I am working with the Web Application recipes. I am currently working on the sen

Polymorphism
Was wondering if someone could explain this a little better to me. I had always assumed that it had

Create comparison matrix?
I want to create a comparison script for forum software and was wondering is there any tutorial or b

Strange cookie problem. setcookie dependant on where user was directed from?
Hello,

I have a website that sets a cookie when a user visits the website. The cookie holds a

writing a screen scraper
Hello,

I'm writing a screen scraper application and want to be able to get absolute addresses

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