我希望在PHP中生成一个长度为16的不重复随机数码,你能告诉我最好的方法吗?我使用以下代码:
$possible = '0123456789';
$code = '';
$i = 0;
while ($i < 14) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
echo($code);
但是那只生成一个随机数。我想要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);
?>
64194616012765
。你说的“30000个随机数”是什么意思? - Mr.Webfor($i=0;$i<3000;$i++){
是一个好的开始... - Wrikken