Jump to content

I could use some help making sure two arrays are really randomized.


cjtemple

Recommended Posts

I have 2 arrays that I create using range from 1 to 110, I then shuffle them to mix them up. The next part is that I want to make sure that if you go through the arrays the values in array 1 are not in the same place in array 2 (plus or minus 1). Here is what I have but it isn't working at all.

$csLength = 110;
$cs = range(1,$csLength);
$reflectorIn = range(1,$csLength);
$reflectorOut = range(1,$csLength);
 
shuffle($cs);
shuffle($reflectorIn);
shuffle($reflectorOut);
 

function generateReflector() {
global $reflectorIn, $reflectorOut;
 
$resultOut = array();
 
for($i = 0; $i < count($reflectorIn); $i++) {
testOutput($i, $reflectorOut[$i]);
}
 
for($i = 0; $i < count($reflectorIn); $i+=2) {
array_push($resultOut, $reflectorOut[$i].",".$reflectorOut[$i+1]);
}
 
echo "<br />Input: \n";
for($i = 0; $i < count ($reflectorIn); $i++) {
echo "<item>".$reflectorIn[$i]."</item>\n";
}
 
echo "<br />Output: \n";
for($i = 0; $i < count($resultOut); $i++) {
echo "<item>".$resultOut[$i]."</item>\n";
}
}
 

function testOutput($currentPosition, $currentValue) {
global $reflectorIn, $reflectorOut;
 
if($currentPosition != 0) {
if($refelctorIn[$currentPosition-1] == $currentValue) {
swapOutput($currentPosition, $currentValue);
}
}
if($currentPosition < count($reflectorOut)) {
if($reflectorIn[$currentPosition+1] == $currentValue) {
swapOutput($currentPosition, $currentValue);
}
}
if($refelectorIn[$currentPosition] == $currentValue) {
swapOutput($currentPosition, $currentValue);
}
}
 

function swapOutput($currentPosition, $currentValue) {
global $reflectorIn, $reflectorOut;
 
$endSwap = false;
while(!$endSwap) {
$keys = array_rand($reflectorOut);
$newKey = $keys[0];
$newValue = $reflectorOut[$newKey];
if($newKey!=0) {
if($reflectorIn[$newKey-1]==$currentValue) {
continue;
}
if($reflectorIn[$newKey-1]==$newValue) {
continue;
}
} else if($newKey<count($reflectorOut)) {
if($reflectorIn[$newKey+1]==$currentValue) {
continue;
}
if($reflectorIn[$newKey+1]==$newValue) {
continue;
}
} else if($reflectorIn[$currentPosition]==$currentValue) {
continue;
} else {
$reflectorOut[$newKey] = $currentValue;
$reflectorOut[$currentPosition] = $newValue;
break;
}
}
}

Reformatted your code and corrected the misspelled reflector instances.

 

$csLength = 110;
$cs = range(1, $csLength);
$reflectorIn = range(1, $csLength);
$reflectorOut = range(1, $csLength);

shuffle($cs);
shuffle($reflectorIn);
shuffle($reflectorOut);


function generateReflector()
{
    global $reflectorIn, $reflectorOut;

    $resultOut = array();

    for ($i = 0; $i < count($reflectorIn); $i++) {
        testOutput($i, $reflectorOut[$i]);
    }

    for ($i = 0; $i < count($reflectorIn); $i += 2) {
        array_push($resultOut, $reflectorOut[$i] . "," . $reflectorOut[$i + 1]);
    }

    echo "<br />Input: \n";
    for ($i = 0; $i < count($reflectorIn); $i++) {
        echo "<item>" . $reflectorIn[$i] . "</item>\n";
    }

    echo "<br />Output: \n";
    for ($i = 0; $i < count($resultOut); $i++) {
        echo "<item>" . $resultOut[$i] . "</item>\n";
    }
}

function testOutput($currentPosition, $currentValue)
{
    global $reflectorIn, $reflectorOut;

    if ($currentPosition != 0) {
        if ($reflectorIn[$currentPosition - 1] == $currentValue) {
            swapOutput($currentPosition, $currentValue);
        }
    }
    
    if ($currentPosition < count($reflectorOut)) {
        if ($reflectorIn[$currentPosition + 1] == $currentValue) {
            swapOutput($currentPosition, $currentValue);
        }
    }
    
    if ($reflectorIn[$currentPosition] == $currentValue) {
        swapOutput($currentPosition, $currentValue);
    }
}

function swapOutput($currentPosition, $currentValue)
{
    global $reflectorIn, $reflectorOut;

    $endSwap = false;
    while (!$endSwap) {
        $keys = array_rand($reflectorOut);
        $newKey = $keys[0];
        $newValue = $reflectorOut[$newKey];
        if ($newKey != 0) {
            if ($reflectorIn[$newKey - 1] == $currentValue) {
                continue;
            }
            if ($reflectorIn[$newKey - 1] == $newValue) {
                continue;
            }
        } else if ($newKey < count($reflectorOut)) {
            if ($reflectorIn[$newKey + 1] == $currentValue) {
                continue;
            }
            if ($reflectorIn[$newKey + 1] == $newValue) {
                continue;
            }
        } else if ($reflectorIn[$currentPosition] == $currentValue) {
            continue;
        } else {
            $reflectorOut[$newKey] = $currentValue;
            $reflectorOut[$currentPosition] = $newValue;
            break;
        }
    }
}

Thank you for spotting the typos, much appreciated. I modified some of my underlining requirements (most notably is I don't want two values that are equal at or plus one if the index is an odd number) and took a different approach that seams to be much more efficient/buggy

 

<?php
$csLength = 110;
$reflectorIn = range(1,$csLength);
$reflectorOut = array();

shuffle($reflectorIn);

echo "<br ><br >Reflector: \n";
generateReflector();

function generateReflector() {
	global $reflectorIn, $reflectorOut;
	
	$clone = $reflectorIn;
	$resultOut = array();
	
	for($i = 0; $i < count($reflectorIn); $i++) {
		while (true) {
			$newKey = array_rand($clone);
			$newValue = $clone[$newKey];
			if($reflectorIn[$i]!=$newValue) {
				if($i+1<count($reflectorIn)) {
					if((($i+1)%2) ==1) {
						array_push($reflectorOut,$newValue);
						break;
					} else {
						if($reflectorIn[$i+1]!=$newValue) {
							array_push($reflectorOut,$newValue);
							break;
						}
						continue;
					}
				} else {
					array_push($reflectorOut,$newValue);
					break;
				}
			} else {
				continue;
			}
		}
		unset($clone[$newKey]);
	}
	
	for($i = 0; $i < count($reflectorIn); $i+=2) {
		array_push($resultOut, $reflectorOut[$i].",".$reflectorOut[$i+1]);
	}
	
	echo "<br />Input: \n";
	for($i = 0; $i < count ($reflectorIn); $i++) {
		echo "<item>".$reflectorIn[$i]."</item>\n";
	}
	
	echo "<br />Output: \n";
	for($i = 0; $i < count($resultOut); $i++) {
		echo "<item>".$resultOut[$i]."</item>\n";
	}
}
?>
One additional note I need to swap my $reflectorOut and $resultOut arrays. The $resultOut needs to be just a temporary array, but the way I am using them above is backwards.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.