开发者

Generate Random String

How do you generate codes that should be 16 digits in length, unique, made up of 1-9, a-z and A-Z (case sensitive obviously so Abc1 is not the same as abc1) ex: Ahs78D7xkEshud45

开发者_StackOverflow中文版

I tried doing this but cannot seem to figure out the easiest way.

Thanks for your help!


If uniqueness isn't of great importance, you can try this:

function randomString() {
    $length = 16;
    $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $str = "";    

    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[mt_rand(0, strlen($chars) - 1)];
    }

    return $str;
}

For a more unique alternative use a hash function:

$str = substr(base64_encode(sha1(mt_rand())), 0, 16);


can use a combination of mt_rand() + md5 + base64 and cut it with substr

try the following code:

<?php

$unique = substr(base64_encode(md5( mt_rand() )), 0, 15);

?>

edit: actualy the md5 bit is not required so the following code will be easier and faster:

<?php

$unique = substr(base64_encode(mt_rand()), 0, 15);

?>

edit2: this produces strings like:

NTA2YWUxODEzNDkw


May be you can try this:

$unique_id = substr(md5(uniqid(rand(), true)), 16, 16);


Don't use a substring of uniqid. It's not guaranteed to be unique; if I gave you a set of unique numbers {101, 102, 103} and you took the first two digits and assumed they would be unique as well...

Really, you're using PHP. Set up a DB table where you store the generated unique codes, index them, and check against them as you make new, completely random codes. It will cost O(log(n)) time, where n is the number of codes you have already generated. If you don't even want that, then store your previously generated codes in a hashtable. But without remembering your previous results you cannot guarantee uniqueness.


$n = rand(10e16, 10e20);
$rand = base_convert($n, 30, 36);

produces like this: 5urp7h2p6a8so0o0


function assign_rand_value($num)
{

// accepts 1 - 36

  switch($num)

  {

    case "1":
     $rand_value = "a";
    break;
    case "2":
     $rand_value = "b";
    break;
    case "3":
     $rand_value = "c";
    break;
    case "4":
     $rand_value = "d";
    break;
    case "5":
     $rand_value = "e";
    break;
    case "6":
     $rand_value = "f";
    break;
    case "7":
     $rand_value = "g";
    break;
    case "8":
     $rand_value = "h";
    break;
    case "9":
     $rand_value = "i";
    break;
    case "10":
     $rand_value = "j";
    break;
    case "11":
     $rand_value = "k";
    break;
    case "12":
     $rand_value = "l";
    break;
    case "13":
     $rand_value = "m";
    break;
    case "14":
     $rand_value = "n";
    break;
    case "15":
     $rand_value = "o";
    break;
    case "16":
     $rand_value = "p";
    break;
    case "17":
     $rand_value = "q";
    break;
    case "18":
     $rand_value = "r";
    break;
    case "19":
     $rand_value = "s";
    break;
    case "20":
     $rand_value = "t";
    break;
    case "21":
     $rand_value = "u";
    break;
    case "22":
     $rand_value = "v";
    break;
    case "23":
     $rand_value = "w";
    break;
    case "24":
     $rand_value = "x";
    break;
    case "25":
     $rand_value = "y";
    break;
    case "26":
     $rand_value = "z";
    break;
    case "27":
     $rand_value = "0";
    break;
    case "28":
     $rand_value = "1";
    break;
    case "29":
     $rand_value = "2";
    break;
    case "30":
     $rand_value = "3";
    break;
    case "31":
     $rand_value = "4";
    break;
    case "32":
     $rand_value = "5";
    break;
    case "33":
     $rand_value = "6";
    break;
    case "34":
     $rand_value = "7";
    break;
    case "35":
     $rand_value = "8";
    break;
    case "36":
     $rand_value = "9";
    break;

  }

return $rand_value;

}

you can put it in loop for multiple time execution and store in string ...

this may help you ..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜