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 Like 43 Dislike
Previous forums Next forums
Other forums

php wont update my db
hello,

sorry for posting in mysql forum but i dont know where exactly is the problem but here

how make 2 column in table with data tybe long row
hi every one

I want make in my table 2 column with data tybe long raw

how thi

Querying info from one table based on info in another
Hi, I am currently trying to make a part for my user driven website where one user can subscribe to

Gerenate tabel in Php
I whant to generate a tabel takeing data from database like this one so the results to be display on

IIS on 2003 anon access disabled issue
I have an IIS server that belongs to a domain, and anonymous access is turned off for all my sites.

Where is my php.ini???
Hi

First off - apologies - i'm not a coder so this may sound very amateur.

I'm having

preg-match with a string and numbers
Hi, I can't get the expression to work for my preg_match, what I'd like is for it to match the strin

PHP loop
Okay so I have several "articles" stored in a mysql database and am attempting to echo tho

pagination numbering pattern
Hello,

I have following code which works great for pagination. but i have small issue now.

Something like an INI editor or a DelimitedText-Editor
Hi all,

Am very, very, very new to PHP and not sure if I should be posting this to a Javascri

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