! How to generate a random number more quickly!

Source: Internet
Author: User
The requirement is this:
Member shopping, will generate an order number, this order number must be a number, and is within a certain range of data, such as 1 to 100,000.

I'm using rand (1,100000), generate a random number, get this random number, then go to the database, if already exist this number, then cycle again.
The code is as follows:
         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);}}


In this way, when the order number is very small, it is easy to get the order number, but if the order number has reached 99990
This time, to generate other order numbers will be a long time, a few minutes or even one hours is possible.

This method is not feasible.

Please friends to help think of ways, or provide other ideas, thank you!


Reply to discussion (solution)

$_order_id = Date (' Ymdhis '). Rand (100000,999999);//Such order number should not be duplicated, and convenient, the front can also see the time to

$_order_id = Date (' Ymdhis '). Rand (100000,999999);//Such order number should not be duplicated, and convenient, the front can also see the time to



requirements, not to be in the number of 1.1 million


$_order_id = Date (' Ymdhis '). Rand (100000,999999);//Such order number should not be duplicated, and convenient, the front can also see the time to



requirements, not to be in the number of 1.1 million



Then you should use the atomic Redis self-increment.



$_order_id = Date (' Ymdhis '). Rand (100000,999999);//Such order number should not be duplicated, and convenient, the front can also see the time to



requirements, not to be in the number of 1.1 million



Then you should use the atomic Redis self-increment.



I did not describe clearly, add it, the order number should be between 1.1 million. The order number can not increment or decrement the generation, must be random, as long as the random between 1.1 million, if the random process of generating a hyphen does not need to be processed

You can write a pseudo-random number generator yourself.
First observe

$m = 100000; $c = 101; $b = Bayi; $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)
It is known that there are 0-99999 randomly arranged numbers in the $r, and each number only appears once

Make $n the last inbound number, ($n * $c + $b)% $m must not be in the library

I hope you can understand.

First check out the existing order number to put the array, there is no need for each library

Query the total number of unused order numbers first, then the maximum number of random numbers to set it, with how much, you limit how much

The member id+ time stamp, the position is not enough to fill up enough random number to be possible. Will never repeat it.

You can write a pseudo-random number generator yourself.
First observe

$m = 100000; $c = 101; $b = Bayi; $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)
It is known that there are 0-99999 randomly arranged numbers in the $r, and each number only appears once

Make $n the last inbound number, ($n * $c + $b)% $m must not be in the library

I hope you can understand.


Can explain why the random number is not repeated, what is the mathematical principle ah, I found a lot of pseudo-random numbers are to use the natural logarithm

Adding time to generate order numbers is also possible.

Linear co-congruential Linear congruence  method is a widely used pseudo-random number generation algorithm, whose basic idea is to get the next number by linear operation on the previous number and modulo. namely: A (i+1) = (A (i) *b+c) mod (m) where B is called a multiplier, C is an increment, M is called modulus, and they are constants. The selection of multiplier, increment and modulus can be varied, as long as the random number is guaranteed to have good uniformity and randomness. The maximum period for linear and congruential is M, but generally less than M. To maximize the cycle, the following conditions should be met: (1) C and M coprime; (2) The product energy of all the mass factors of M is divisible by b-1; (3) If M is a multiple of 4, then B-1 is also; (4) b,c,a (0) (initial value, generally the seed) is smaller than M; (5)
After a patient screening, can be found to make absolutely uniform B and C

Array_count_values (Array_count_values ($r))
Array_count_values ($r) Gets an array that holds the number of occurrences of each value in
Again the statistics were
Array
(
[1] = 100000
)
These 100,000 numbers are not duplicated.

$_order_id = Date (' Ymdhis '). Rand (1000,9999);

Thanks for the helpful help of the moderator

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.