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

Why does it need to be this complicated? PHP has builtins for changing the seed of the random number generator, and for shuffling strings.

function shuffleString($input, $seed = 2000) {
    srand($seed); // set the random generator seed
    $shuffled = str_shuffle($input);
    srand(); // to ensure that the seed is reset
    return $shuffled;
}

This might be less efficient (I haven't benchmarked them) but it's simpler and more readable. Sometimes it's fun to reinvent the wheel, but there are usually shortcuts that can be taken.

  • As written now, this function appears to always return a completly random shuffle. – Hoytman Aug 10 at 15:24
  • I made a variable name error in the code, but bishop fixed it. – maxb Aug 13 at 5:11

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.