如何在php中生成唯一的随机数?
我使用php随机函数生成随机数,但我希望生成的数字应该是唯一的,并且不应该再次重复如何在php中生成唯一的随机数?,php,Php,我使用php随机函数生成随机数,但我希望生成的数字应该是唯一的,并且不应该再次重复 ---------- php code $number = rand(100,100000); //a six digit random number between 100 to 100000 echo $number; ---------- 但我在为用户编写的代码中多次使用此函数,因此在极少数情况下,应该有机会再次生成相同的数字。如何避免这种情况。有很多方法,例如: $freq = []; $numb
----------
php code
$number = rand(100,100000); //a six digit random number between 100 to 100000
echo $number;
----------
但我在为用户编写的代码中多次使用此函数,因此在极少数情况下,应该有机会再次生成相同的数字。如何避免这种情况。有很多方法,例如:
$freq = [];
$number = rand(100,100000);
$times = 10;
while($times-- > 0)
{
while(in_array($number, $freq))$number = rand(100,100000);
$freq[] = $number;
echo $number . "<br>";
}
$freq=[];
$number=兰特(100100000);
$times=10;
而($times-->0)
{
而(在数组中($number,$freq))$number=rand(100100000);
$freq[]=$number;
回显$number.“
”;
}
这将打印10个随机唯一数字。有很多方法,例如:
$freq = [];
$number = rand(100,100000);
$times = 10;
while($times-- > 0)
{
while(in_array($number, $freq))$number = rand(100,100000);
$freq[] = $number;
echo $number . "<br>";
}
$freq=[];
$number=兰特(100100000);
$times=10;
而($times-->0)
{
而(在数组中($number,$freq))$number=rand(100100000);
$freq[]=$number;
回显$number.“
”;
}
这将打印10个随机唯一的数字。您只需在php中使用时间戳,因为时间戳不会相互交叉,因此它将始终生成唯一的数字。您可以在php中使用
time()
函数
time()
函数用于将时间戳格式化为所需格式。时间戳是当前时间与1970年1月1日00:00:00 GMT之间的秒数。它也称为UNIX时间戳
<?php
$t=time();
echo $t;
?>
以上将减少时间戳冲突的机会,从而使数字唯一性的概率几乎达到100%
如果您在数据库中使列唯一
,则php不会插入数字,因此此瓶颈将确保您始终获得唯一的随机数字
bill_id not null unique
您需要做的就是在php中使用时间戳,因为时间戳不会相互交叉,因此它将始终生成唯一的数字。您可以在php中使用
time()
函数
time()
函数用于将时间戳格式化为所需格式。时间戳是当前时间与1970年1月1日00:00:00 GMT之间的秒数。它也称为UNIX时间戳
<?php
$t=time();
echo $t;
?>
以上将减少时间戳冲突的机会,从而使数字唯一性的概率几乎达到100%
如果您在数据库中使列唯一
,则php不会插入数字,因此此瓶颈将确保您始终获得唯一的随机数字
bill_id not null unique
如果您使用它来表示用户id之类的内容,那么您可以使用
uniqid
。此命令根据当前时间(以微秒为单位)获取前缀的唯一标识符
以下是如何使用它:
string uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE]] )
如果同时为大量if主机生成id,则使用前缀,如果在同一微秒生成id,则可以使用前缀来区分不同的主机
熵越大,获得唯一值的相似性越大
用法:
<?php
/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());
/* We can also prefix the uniqid, this the same as
* doing:
*
* $uniqid = $prefix . uniqid();
* $uniqid = uniqid($prefix);
*/
printf("uniqid('php_'): %s\r\n", uniqid('php_'));
/* We can also activate the more_entropy parameter, which is
* required on some systems, like Cygwin. This makes uniqid()
* produce a value like: 4b340550242239.64159797
*/
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>
如果您将其用于类似用户id的内容,那么您可以使用uniqid
进行此操作。此命令根据当前时间(以微秒为单位)获取前缀的唯一标识符
以下是如何使用它:
string uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE]] )
如果同时为大量if主机生成id,则使用前缀,如果在同一微秒生成id,则可以使用前缀来区分不同的主机
熵越大,获得唯一值的相似性越大
用法:
<?php
/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());
/* We can also prefix the uniqid, this the same as
* doing:
*
* $uniqid = $prefix . uniqid();
* $uniqid = uniqid($prefix);
*/
printf("uniqid('php_'): %s\r\n", uniqid('php_'));
/* We can also activate the more_entropy parameter, which is
* required on some systems, like Cygwin. This makes uniqid()
* produce a value like: 4b340550242239.64159797
*/
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>
我会这样做:
你说你有分支。收据id可能如下所示:
$dateString = date('Ymd'); //Generate a datestring.
$branchNumber = 101; //Get the branch number somehow.
$receiptNumber = 1; //You will query the last receipt in your database
//and get the last $receiptNumber for that branch and add 1 to it.;
if($receiptNumber < 9999) {
$receiptNumber = $receiptNumber + 1;
}else{
$receiptNumber = 1;
}
内容如下:
20180406-101-1
这将是独一无二的(前提是您每天的交易少于10000笔),并将向您的员工显示易于阅读的信息。我会这样做:
你说你有分支。收据id可能如下所示:
$dateString = date('Ymd'); //Generate a datestring.
$branchNumber = 101; //Get the branch number somehow.
$receiptNumber = 1; //You will query the last receipt in your database
//and get the last $receiptNumber for that branch and add 1 to it.;
if($receiptNumber < 9999) {
$receiptNumber = $receiptNumber + 1;
}else{
$receiptNumber = 1;
}
内容如下:
20180406-101-1
这将是唯一的(如果您每天的交易少于10000笔),并将向您的员工显示易于阅读的信息。
如果要将用户存储在DB中,则应使用自动增量将列[ID]创建为主键,这将是最佳解决方案
在另一种情况下,我建议您通过读取最后一个id并向其中添加1,简单地以从N到M的升序存储所有用户id,因为随机顺序只会增加代码的复杂性,因此我看不到任何实际收益。如果您将用户存储在DB中,则应创建列[id]作为主键自动递增,这将是最好的解决方案
在另一种情况下,我建议您通过读取最后一个id并向其中添加1,以从N到M的升序简单地存储所有用户id,因为随机顺序只会增加代码的复杂性,因此我看不到任何实际收益。此代码必须有效
关于代码的一些说明:
生成唯一id
使用正则表达式从唯一id提取数字
使用循环从正则表达式收集数字
此代码必须有效
关于代码的一些说明:
生成唯一id
使用正则表达式从唯一id提取数字
使用循环从正则表达式收集数字
存储编号,检查其以前是否使用过。只有100%可靠的随机数方法使用它的目的是什么?为客户制作唯一的id您是否将其存储在数据库中?你可以使用主键,最好的方法是-1。使用rand()
生成e随机数。2.抓取当前的时间戳。3.添加一个前缀
,该前缀可能是一个字符串,具体取决于您的业务逻辑。4.将这三者结合起来,得到一个随机唯一的数字!此过程可确保获得相同号码的准确性。存储号码时,请检查以前是否使用过该号码。只有100%可靠的随机数方法使用它的目的是什么?为客户制作唯一的id您是否将其存储在数据库中?你可以使用主键,最好的方法是-1。使用rand()
生成e随机数。2.抓取当前的时间戳。3.添加一个前缀
,该前缀可能是一个字符串,具体取决于您的业务逻辑。4.康比