dongshu9458 2014-02-12 19:23
浏览 53
已采纳

php函数生成随机字符串连续返回重复值

I have written a function to generate a random string of 7 alphanumeric characters which I am then inserting in a mysql database.

Here is the code :

function getRandomID(){
   $tmp ="";
   $characters=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9");
   for($i=0;$i<7;$i++) 
       $tmp.=$characters[rand(0,count($characters)-1)];
   return $tmp;
}

I am not checking for duplicates atm because I anticipate there will be no more than 1000 entries in the database and I've calculated that this function can return (35)^7 = 64,339,296,875 possible values.

I am testing it out locally as well as on a live server.

The problem is just in the last hour , this function generated duplicate values twice.

I came upon 3 entries in the database all of which had the same random string.

I do not know what could have caused this as I tried numerous times afterwards and the problem wasn't reproducible.

Does anybody have any idea what could be going on here?

Many thanks in advance

  • 写回答

4条回答 默认 最新

  • douquan2023 2014-02-12 19:33
    关注

    Designing your code with the mindset of "meh, that's not going to happen" is a very risky game, just do it properly once so you don't have to get back to your code multiple times to quick-fix minor things like these.

    Do the duplicate check and you'll be solid.

    You can create a function like

    function stringExists($string)
    {
         ...
        return $boolValue;
    }
    

    And you can easily create a while loop that generates a new string while an old one has been generated.

    $duplicate = true;
    while($duplicate)
    {
        $newString = getRandomId();
        $duplicate = !stringExists($string);
    }
    // Work with the newest string that is not a duplicate.
    

    If you really want to get into it

    You can then take a look at the documentation for rand if you want to find out what might be causing your problem. Besides, 3 entries doesn't mean anything if we don't know how many total entries there are. Also sometimes "random" function are not as random as one might think, sometimes random functions in some programming languages are always usable but require some sort of an initiation before they become "truly" random.

    The time of the inserts might also be a part of the problem, there are plenty of threads on the internet, like this one on stackoverflow, that have some interesting points that can affect your "random"ness.

    Whether it's true or not, not which has been pointed out in the comment, you can be pretty sure to find an answer to your question in related threads and topics.

    Short answer: Don't think about it and do a duplicate check, it's easy.

    Note that you should, of-course, make your ID be a UNIQUE constraint in the database to begin with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function