Home  >  Article  >  Backend Development  >  PHP random username account generation

PHP random username account generation

巴扎黑
巴扎黑Original
2016-12-07 13:44:462402browse

The difficulty in random generation is how to avoid collisions. Some people say to use mechanisms such as md5 and GUID. Of course you can, but as an account, it looks messy, and the number of generated digits is too long.

This method only needs to solve concurrent collisions within 1 second, because the fixed head uses unixtime time, which is accurate to the second. If it exceeds 1 second, the time of this head must have changed. At the same time, this method can also generate random order numbers for users.

The test condition is to use a loop to create 10,000 random accounts (millisecond level), with 0 collisions, and 100,000 accounts with approximately 0-3 collisions. There should be no Internet company that would create so many accounts at the same time in an instant, so it is enough to handle a scale of billions of PV.

~~~.java
// Automatically generate random user names
// Test, create 10,000 random accounts in a loop, 0 collisions, 100,000 about 0-3 collisions, enough to handle billions of PV in the future
private function genUserNumber()
{
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$username = "";
for ( $i = 0; $i < 6; $i++ )
{
$username .= $chars[mt_rand( 0, strlen($chars))];
}
return strtoupper(base_convert(time() - 1420070400, 10, 36)).$username;
}
~~~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn