php 如何生成一个 6 位的唯一编号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5464906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i generate a 6 digit unique number?
提问by AAA
How can i generate a 6 digit unique number? I have verification mechanisms in place to check for duplicate entries.
如何生成一个 6 位的唯一编号?我有验证机制来检查重复的条目。
回答by Charles
$six_digit_random_number = mt_rand(100000, 999999);
As all numbers between 100,000 and 999,999 are six digits, of course.
当然,100,000 和 999,999 之间的所有数字都是六位数。
回答by Tim Cooper
If you want it to start at 000001
and go to 999999
:
如果您希望它从以下位置开始000001
并转到999999
:
$num_str = sprintf("%06d", mt_rand(1, 999999));
Mind you, it's stored as a string.
请注意,它存储为字符串。
回答by Frosty Z
Another one:
另一个:
str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
Anyway, for uniqueness, you will have to check that your number hasn't been already used.
无论如何,为了唯一性,您必须检查您的号码是否已被使用。
You tell that you check for duplicates, but be cautious since when most numbers will be used, the number of "attempts" (and therefore the time taken) for getting a new number will increase, possibly resulting in very long delays & wasting CPU resources.
你告诉你检查重复,但要小心,因为当大多数数字将被使用时,获取新数字的“尝试”次数(以及因此花费的时间)会增加,可能会导致很长的延迟并浪费 CPU 资源.
I would advise, if possible, to keep track of available IDs in an array, then randomly choose an ID among the available ones, by doing something like this (if ID list is kept in memory):
如果可能的话,我会建议跟踪数组中的可用 ID,然后在可用 ID 中随机选择一个 ID ,方法如下(如果 ID 列表保存在内存中):
$arrayOfAvailableIDs = array_map(function($nb) {
return str_pad($nb, 6, '0', STR_PAD_LEFT);
}, range(0, 999999));
$nbAvailableIDs = count($arrayOfAvailableIDs);
// pick a random ID
$newID = array_splice($arrayOfAvailableIDs, mt_rand(0, $nbAvailableIDs-1), 1);
$nbAvailableIDs--;
You can do something similar even if the ID list is stored in a database.
即使 ID 列表存储在数据库中,您也可以执行类似的操作。
回答by virus
<?php
$file = 'count.txt';
//get the number from the file
$uniq = file_get_contents($file);
//add +1
$id = $uniq + 1 ;
// add that new value to text file again for next use
file_put_contents($file, $id);
// your unique id ready
echo $id;
?>
i hope this will work fine. i use the same technique in my website.
我希望这会正常工作。我在我的网站上使用相同的技术。
回答by law.vzmk
Here's another one:
这是另一个:
substr(number_format(time() * rand(),0,'',''),0,6);
回答by Ananta Prasad
This will generate random 6 digit number
这将生成随机的 6 位数字
<?php
mt_rand(100000,999999);
?>
回答by THEMBO CHARLES LWANGA
$characters = '123456789';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 6; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$pin=$randomString;
回答by Yes Barry
In PHP 7.0+ I would suggest random_int($min, $max)
overmt_rand()
.
在 PHP 7.0+ 中,我建议random_int($min, $max)
超过mt_rand()
.
$randomSixDigitInt = \random_int(100000, 999999);
From php.net:
来自 php.net:
Caution This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.
注意 此函数不会生成加密安全值,不应用于加密目的。如果您需要加密安全值,请考虑改用 random_int()、random_bytes() 或 openssl_random_pseudo_bytes()。
So this depends mostly on context. I'll also add that as of PHP 7.1.0 rand()
is now an alias to mt_rand()
.
所以这主要取决于上下文。我还要补充一点,从 PHP 7.1.0 开始rand()
,现在是mt_rand()
.
Cheers
干杯
回答by Manjeet Kumar Nai
<?php echo rand(100000,999999); ?>
<?php echo rand(100000,999999); ?>
you can generate random number
你可以生成随机数
回答by rossb83
I would use an algorithm, brute force could be as follows:
我会使用一种算法,蛮力可能如下:
First time through loop: Generate a random number between 100,000 through 999,999 and call that x1
第一次循环:生成一个介于 100,000 到 999,999 之间的随机数,并称其为 x1
Second time through the loop Generate a random number between 100,000 and x1 call this xt2, then generate a random number between x1 and 999,999 call this xt3, then randomly choose x2 or x3, call this x2
第二次通过循环生成100,000和x1之间的随机数调用这个xt2,然后生成一个x1和999,999之间的随机数调用这个xt3,然后随机选择x2或x3,调用这个x2
Nth time through the loop Generate random number between 100,000 and x1, x1 and x2, and x2 through 999,999 and so forth...
通过循环第 N 次生成 100,000 和 x1、x1 和 x2 以及 x2 到 999,999 之间的随机数等等......
watch out for endpoints, also watch out for x1
注意端点,还要注意 x1