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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等