cjtemple Posted November 5, 2013 Share Posted November 5, 2013 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; } } } Link to comment https://forums.phpfreaks.com/topic/283626-i-could-use-some-help-making-sure-two-arrays-are-really-randomized/ Share on other sites More sharing options...
requinix Posted November 5, 2013 Share Posted November 5, 2013 Did notice you typoed "reflector" a few times. Link to comment https://forums.phpfreaks.com/topic/283626-i-could-use-some-help-making-sure-two-arrays-are-really-randomized/#findComment-1457094 Share on other sites More sharing options...
ignace Posted November 5, 2013 Share Posted November 5, 2013 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; } } } Link to comment https://forums.phpfreaks.com/topic/283626-i-could-use-some-help-making-sure-two-arrays-are-really-randomized/#findComment-1457095 Share on other sites More sharing options...
cjtemple Posted November 5, 2013 Author Share Posted November 5, 2013 You know I noticed the typo else where, I will have to poor through it a bit to look for the others, and I bet it will dramatically effect the results. Link to comment https://forums.phpfreaks.com/topic/283626-i-could-use-some-help-making-sure-two-arrays-are-really-randomized/#findComment-1457098 Share on other sites More sharing options...
cjtemple Posted November 6, 2013 Author Share Posted November 6, 2013 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. Link to comment https://forums.phpfreaks.com/topic/283626-i-could-use-some-help-making-sure-two-arrays-are-really-randomized/#findComment-1457240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.