duanqi5333 2013-07-18 09:23
浏览 32
已采纳

什么是在PHP中生成16长度随机数的最佳方法? [关闭]

I want to generate a random number code in PHP without repeat and with 16 lengths. What's the best way to do this? I use this code :

$possible = '0123456789';
$code = '';
$i = 0;
while ($i < 14) {
    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
    $i++;
}
echo($code);

But that is generating 1 random number. I want 30000 random numbers. What shall I do ?

i use this code too but that is not generate 16 length :

<?php

    $con=mysqli_connect("localhost","root","","test1");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysql_connect_error();
    }
    for ($s=0;$s<10;$s++) {
        $possible = '0123456789';
        $code = '';
        $i = 0;
    while ($i < 16) {
        $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
        $i++;
    }
    echo($code);
    echo nl2br("$code <br/>");

    mysqli_query($con,"INSERT INTO test (ID, Code, Type, Used)
    VALUES ('', '".$code."','1', '0')");
    }
    for ($s=0;$s<10;$s++) {
        $possible = '0123456789';
        $code = '';
        $i = 0;
    while ($i < 16) {
        $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
        $i++;
    }
    echo($code);
    echo nl2br("$code <br/>");

    mysqli_query($con,"INSERT INTO test (ID, Code, Type, Used)
    VALUES ('', '".$code."','2', '0')");
    }
    for ($s=0;$s<10;$s++) {
        $possible = '0123456789';
        $code = '';
        $i = 0;
    while ($i < 16) {
        $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
        $i++;
    }
    echo($code);
    echo nl2br("$code <br/>");

    mysqli_query($con,"INSERT INTO test (ID, Code, Type, Used)
    VALUES ('', '".$code."','3', '0')");
    }
    mysqli_close($con);
?>
  • 写回答

2条回答 默认 最新

  • dongzhan1492 2013-07-18 09:36
    关注

    Snippet for no repeating digits:

    $len=14;
    $last=-1;
    for ($i=0;$i<$len;$i++)
    {
        do
        {
            $next_digit=mt_rand(0,9);
        }
        while ($next_digit == $last);
        $last=$next_digit;
        $code.=$next_digit;
    }
    

    Snippet for no duplicates within 30000 (but repeating digits are allowed):

    $codes=array();
    while (count($codes) < 30000)
    {
        $code=rand(pow(10,13),pow(10,14)-1);
        $codes["$code"]=1;
    }
    

    The codes are stored as the keys of the array (just to save some memory).

    There might be better solutions for this problem (especially with more efficient ones) - but this one is really short ;-) - due to the integration of Kolink's solution.

    Hope it helps a bit.

    *Jost

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

报告相同问题?

悬赏问题

  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件