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 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥15 为什么我通过html绘制的SVG折线图插入到word中坐标轴不显示出来
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少