gpt4 book ai didi

php - 在 php 中生成 16 长度随机数的最佳方法是什么?

转载 作者:可可西里 更新时间:2023-11-01 01:03:14 25 4
gpt4 key购买 nike

<分区>

我想在 PHP 中生成一个不重复且长度为 16 的随机数代码。最好的方法是什么?我使用这段代码:

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

但这是生成 1 个随机数。我想要 30000 个随机数。我该怎么办?

我也使用此代码,但不会生成 16 位长度:

<?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);
?>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com