!如何更快速的生成一个随机数!
需求是这样的:
会员购物,会生成一个定单号,这个定单号必须是数字,而且是在一定的范围之内的数据,如 1 到 10万。
我现在用的方法是 用 rand(1,100000),生成一个随机数,得到这个随机数后,再去数据库里查,如果已经存在这个数了,就循环再查。
代码如下:
set_time_limit(0); $test = M('test'); $_run = true; $_order_id = rand(0, 100000); while ($_run) { $orderInfo = $test->where("val = ".$_order_id)->find(); if(empty($orderInfo)){ $_run = false; return $_order_id; }else{ $_order_id = rand(0, 100000); } }
这样做了,在定单号还很少的时候,很轻松的就取得了定单号,可假如当定单号已经达到了99990
这个时间,要生成其它的定单号就要很久了,几分钟甚至一个小时都有可能。
这种方法行不通了。
请朋友们帮想想办法,或提供下其它的思路,谢谢了!
回复讨论(解决方案)
$_order_id = date('YmdHis') . rand(100000,999999);//这样的订单号应该不会有重复的,又方便,前面还能看出时间来
$_order_id = date('YmdHis') . rand(100000,999999);//这样的订单号应该不会有重复的,又方便,前面还能看出时间来
需求要求,非要在1-10万之间的数
$_order_id = date('YmdHis') . rand(100000,999999);//这样的订单号应该不会有重复的,又方便,前面还能看出时间来
需求要求,非要在1-10万之间的数
那你还是用原子性的redis自增吧
$_order_id = date('YmdHis') . rand(100000,999999);//这样的订单号应该不会有重复的,又方便,前面还能看出时间来
需求要求,非要在1-10万之间的数
那你还是用原子性的redis自增吧
我没有描述清楚,补充下吧,定单号要在1-10万之间。 定单号不能 递增或递减的生成,一定要随机的,只要在1-10万之间随机,如果随机的过程中产生连号不用处理
可以自己写一个伪随机数发生器
先观察
$m = 100000;$c = 101;$b = 81;$n = 0;for($i=0; $i<100000; $i++) { $n = ($n * $c + $b) % $m; $r[] = $n;}print_r(array_count_values(array_count_values($r)));
Array( [1] => 100000)
令 $n 为最后一次入库的号码,则 ($n * $c + $b) % $m 一定就不在库中
希望你能理解
先把已有订单号查询出来放数组,没必要每次查库
先查询没有使用的订单号总量,然后随机数最大值就设它,随到多少,你就limit多少
会员ID+时间戳,位置不够在补上足够的随机数就可以。永远也不会重复
可以自己写一个伪随机数发生器
先观察
$m = 100000;$c = 101;$b = 81;$n = 0;for($i=0; $i<100000; $i++) { $n = ($n * $c + $b) % $m; $r[] = $n;}print_r(array_count_values(array_count_values($r)));
Array( [1] => 100000)
令 $n 为最后一次入库的号码,则 ($n * $c + $b) % $m 一定就不在库中
希望你能理解
能解释下为何是不重复的随机数吗,数学原理是什么啊,我找到不少伪随机数都是要用自然对数的
加时间生成订单号也是可以的
线性同余法 线性同余方法是目前应用广泛的伪随机数生成算法,其基本思想是通过对前一个数进行线性运算并取模从而得到下一个数。即:a(i+1)=(a(i)*b+c)mod(m)其中b称为乘数,c称为增量,m称为模数,它们均为常数。乘数、增量和模数的选取可以多种多样,只要保证产生的随机数有较好的均匀性和随机性即可。线性同余法的最大周期是m,但一般情况下会小于m。要使周期达到最大,应该满足以下条件:(1) c和m互质;(2) m的所有质因子的积能整除b-1;(3) 若m是4的倍数,则b-1也是;(4) b,c,a(0)(初值,一般即种子)都比m小;(5) b,c是正整数。
array_count_values(array_count_values($r)) 中
array_count_values($r) 得到一个数组,保存有$r中每个值出现的次数
再次统计得
Array
(
[1] => 100000
)
可知这十万个数是不重复的
$_order_id = date('YmdHis') . rand(1000,9999);
谢谢版主的热心帮助

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
