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

CODE NOT WORKING
Code: [Select]<?php
//include shared codes

include '../lib/common.php';
include

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 good snippet manager
Hi. as i am coding and learning more, it is becoming difficult to manage code snippets. how you mana

PHP and Javascript
Hi Guys,

Im trying to load the contents of an html file into a text area for editing, this is

Opening Multiple Files/Links in Order.
I want to open links in order/one-by-one and check each for a specific string.

Example:
I

Why doesn't this work? (SSH2)
This is my script:

Code: <?php
$connection = ssh2_connect('213.251.167.109', 22);

Take info from one coloum and move to another
Hi all,

I have this:
Code: [Select]$array = "SELECT stock_id FROM stocks WHERE stock_

Strange PHP/mySQL error ... am I just tired?
Code: <?

## CONNECT TO DB FUNCTION!
function ConnectTo($db2con)
{
$hostNam

Port scanner problem
Hai
recently i developed one app through which u can check the opened and closed ports under an

What are causes of a connection-timeout with fopen()?
Hello! Here is the situation: The server I host my website on just upgraded it's PHP build from 4.4.

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