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
Find only certain URLs from page ... regex (semi-complete script)
Hi guys,
What I need to do is take a page & extract all the URLs from the page &a
Users and Groups with PHP Classes
Hi,
I have setup what I hope to be a good working User class. Now I want to introduce group m
Inserting 100x and 100y coordinates in mysql table - easy
Is there a way to insert this data into a mysql table named 'map' with the fields: 'x int 3' , 'y in
storing video files into mysql in php
hi
i have my video files in my folder ,
i have to store the path of the videos into db an
Php WordPress help
I am writing the following code for making a plugin
<?php
header("Content-Type
Server side $_SESSION
how does one keep the session completely server side. no cookies to the browser at all. i need this
split string
Hi all
i have some names (imploded by comma):
Code: toronto,paris,madrid
Now, i would
please help me in this update statment
hi every one
if I have table and this data in it
id name
10
Progress bar tracking file downloads
Hey guys, was wondering if there was a way to track how far a file has been downloaded. I know there
php ajax - two drop down list with a submit button
i have created a form with 2 drop-down list and a submit button. I would like to know how to pass th