How do I generate a number of fixed digits that are not duplicated in the database?

Source: Internet
Author: User
Tags repetition time in milliseconds
Randomly generate a fixed number of digits (for example, 8 digits),

The UID that is registered as a user is stored in the database

Each UID is not duplicated,

But it could be 12345678,12345677.

There's a way to randomly generate 8-bit numbers,

Then query the database for this data,

There is a rebuild in the query,

None is written to the database,

This is a more time-consuming way.

There's no better way,

such as the combination of other random values, timestamps and the like.

Solve the problem idea or method, can be implemented by PHP

Reply content:

Randomly generate a fixed number of digits (for example, 8 digits),

The UID that is registered as a user is stored in the database

Each UID is not duplicated,

But it could be 12345678,12345677.

There's a way to randomly generate 8-bit numbers,

Then query the database for this data,

There is a rebuild in the query,

None is written to the database,

This is a more time-consuming way.

There's no better way,

such as the combination of other random values, timestamps and the like.

Solve the problem idea or method, can be implemented by PHP

Usually the database is stored in the timestamp, that is, the current system time in milliseconds, and then divided by 1000, the current time to get the number of seconds as the UID, because if you want to store milliseconds, it takes up the database is too long, in seconds can be guaranteed unique non-repetition. If you want the generated UID to be more than just a number, you can turn the number of seconds into 16 binary.

User Registration UID, in a range of self-increase can be.
Assuming that the user registration of the QPS is not so high, you can take advantage of update+ private complex data method in MongoDB to simulate a lock, lock protection "UID of the last registered user" data, each take the lock, add one, register, lock the protection of the uid+1, release the lock.

Single, non-sequential: MD5 (UNIQID ());
Order unique: Self-increment

With timestamps, there is little chance of repetition.

//生成0到1的随机小数public  function randomFloat($min = 0, $max = 1) {    return $min + mt_rand() / mt_getrandmax() * ($max - $min);}

Why are you doing this? Do you want to increment from the primary key?

The UID is best to use the primary key to increase the bar, and then the PHP side with mysql_insert_id() such functions to take. This makes the query operation less.

Create a self-increment column, starting with a value of 10000000.

I am glad to answer this question for LZ.

First of all, is there anything that keeps incrementing and doesn't repeat?

Time.

As the LZ suspects, using a time-cut to convert to a number produces a number that is always incremented and does not repeat.

And, these numbers can also be based on time, look at the size of the registration and registration time.

Here is just a point.

$now=date("Y-m-d H:i:s");//按照 年月日,时分秒的格式,记录当前时间。$unix_stamp=strtotime($now);echo $unix_stamp;/*strtotime函数预期一个包含美国英语日期格式的字符串,并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)这里LZ大概会得到一个10位的数字。*/

LZ can use $unix_stamp as a random uid, if the number of seconds is not enough to produce a random number.

(for example, more than 1 seconds of registration), you can add Microtime () function to the number after $unix_stamp.

To make this number more random to avoid the problem of UID duplication.

  • 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.