The following function is designed to accept a string of characters and an integer. The function returns a string that's a reordered version of the original string. The reordering process is controlled by the second variable. If the function is called again with the same string and a different number, a different string will be returned. However, if the string and the integer do not change, repeat calls to this function will always return the same string. as the integer changes, the returned strings should not appear to be similar. I have tested this function on the following string, with integer values between 0 and 40000 without finding a duplicate returned string.
"1234567890abcdefghijklmnopqrstuvwxyz"
Are there obvious improvements that can be made? I am also curious about this function's limitations. So far, it seems to perform rather well.
function shuffleString($inputString, $seed = 2000){
$strLength = strlen($inputString);
$newString = "";
$factor = 3 + (int)($seed / $strLength);
while($strLength > 0){
$seed += $factor;
if($seed >= $strLength){
$seed = $seed % $strLength;
}
$newString .= $inputString[$seed];
$copyStr = "";
$strLength --;
if($seed != 0){
$copyStr = substr($inputString,0,$seed);
}
if($seed != $strLength){
$copyStr .= substr($inputString,$seed+1);
}
$inputString = $copyStr;
}
return $newString.$inputString;
}
Example:
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 1234), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 1234), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 1234), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 1234), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 4567), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 4567), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 4567), '<br />';
echo shuffleString("1234567890abcdefghijklmnopqrstuvwxyz", 4567), '<br />';
returns
beint19kw0q7sf4xrpv3dyjgl2ozh58m6cau
beint19kw0q7sf4xrpv3dyjgl2ozh58m6cau
beint19kw0q7sf4xrpv3dyjgl2ozh58m6cau
beint19kw0q7sf4xrpv3dyjgl2ozh58m6cau
g6yvx5fujbaitkdm2s3oqcnl074hr8wp19ze
g6yvx5fujbaitkdm2s3oqcnl074hr8wp19ze
g6yvx5fujbaitkdm2s3oqcnl074hr8wp19ze
g6yvx5fujbaitkdm2s3oqcnl074hr8wp19ze
also:
for($i=0; $i<5; $i++){
$dataStr = "1234567890abcdefghijklmnopqrstuvwxyz";
for($j=0; $j<2000; $j++){
$dataStr = shuffleString($dataStr, ord($dataStr[0]));
}
echo $dataStr . '<br />';
}
returns:
gdwy63zmnjxharpovk50qulf2cei8t174bs9
gdwy63zmnjxharpovk50qulf2cei8t174bs9
gdwy63zmnjxharpovk50qulf2cei8t174bs9
gdwy63zmnjxharpovk50qulf2cei8t174bs9
gdwy63zmnjxharpovk50qulf2cei8t174bs9