Home > Backend Development > PHP Tutorial > PHP 随机用户名账号的生成

PHP 随机用户名账号的生成

WBOY
Release: 2016-06-20 12:45:28
Original
1081 people have browsed it

随机生成难点是在于如何避免碰撞,有人说用md5,GUID这些机制啊,当然可以,但是做为账号,看着有乱,而且生成位数也过长。本方法只需要解决1秒内的并发碰撞就可以,因为固定头部采用的是unixtime时间,精确到秒,超过1秒,这个头部的时间肯定变化了。同时本方法也可以用户随机订单编号的生成。测试条件是用循环创建1万个随机账号(毫秒级),0碰撞,10万个大约有0-3个碰撞。应该没有哪个互联网公司会在瞬间同时创建这么多账号,所以足够应付数十亿PV这么大的规模了。~~~.java// 自动生成随机用户名// 测试,循环创建1万个随机账号,0碰撞,10万大约0-3个碰撞,足够应付未来数十亿级PVprivate 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;}~~~
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template