宝哥软件园

PHP随机数C扩展随机数

编辑:宝哥软件园 来源:互联网 时间:2021-11-01

因为使用的是固定长度的随机字符串。

第一个是PHP代码。

$ str _ MD5=MD5(uniqid());$rand=mt_rand(1,28);$str1=substr($str_md5,$rand,6);$rand=mt_rand(1,28);$str2=substr($str_md5,$rand,6);$rand=mt_rand(1,28);$str3=substr($str_md5,$rand,6);$code=substr($str1。$str2。$str3,0,8);

生成180,000个随机字符串,根据图中的重复次数以相反的顺序排列。可以看出基本都是重复的。但也很理想。

因为想提高C语言能力,所以用C语言重写了随机生成的字符串。

使用随机数函数srand()和rand()。

但是,过了一两个小时,随机数还是有问题。并发访问的时间可能几乎相同,所以srand给出的种子时间可以认为是相同的。因此,生成的随机数是相同的。产生的随机字符串是相同的。循环输出几乎相同的随机字符串。

后来,我想到了ukey。这个扩展可以实现一个唯一的标识,所以所有的访问都会生成一个唯一的标识。有没有可能用这个ID作为种子时间?答案是肯定的。

上图显示了生成的随机字符串,其长度可以自定义。您也可以输出只有数字的字符串。与PHP相比,PHP生成的随机字符串重复率更低,速度更快。

PHP _ FUNCTION(get _ random _ num _ str){ int length=8;if(ZEND _ parse _ parameters(ZEND _ NUM _ ARGS)(TSRMLS _ CC,' l ',长度)===FAILURE){ 0长度=8;}长度;(同Internationalorganizations)国际组织标志,我;char * string__uint64_t时间戳=实时();_ _ uint 64 _ t retvalint lenchar buf[128];如果(时间戳==0ULL){ RETURN _ FALSE;}自旋锁(锁定,PID);if(context-last _ timestamp==timestamp){ context-sequence=(context-sequence 1)context-sequence _ mask;if(context-sequence==0){ timestamp=skip _ next _ millis();} } else { context-sequence=0;/*回零*/}上下文-last_timestamp=时间戳;retval=((timestamp-context-twe poch)context-timestamp _ left _ shift)|(context-data center _ id context-data center _ id _ shift)|(worker _ id context-worker _ id _ shift)| context-sequence;spin_unlock(锁定,PID);//printf('%ld ',retval);srand((无符号)retval);//srand((无符号)时间(空));if((string=(char *)emalloc(length))==NULL){//myLog(' Malloc失败!标志:14 n ');RETURN _ NULL();} for(I=0;我长度-1;I){ flag=rand()% 3;switch(flag){ case 0: string[I]=' 1 ' rand()% 5;打破;案例1:字符串[I]=' 2 '兰特()% 7;打破;案例2:字符串[I]=' 0 ' rand()% 10;打破;默认值:字符串[I]=' 9 ';打破;} }字符串[length-1]=“0”;RETURN_STRINGL(字符串,长度,0);} PHP _ FUNCTION(get _ random _ str){ int length=8;if(ZEND _ parse _ parameters(ZEND _ NUM _ ARGS)(TSRMLS _ CC,' l ',长度)===FAILURE){ 0长度=8;}长度;(同Internationalorganizations)国际组织标志,我;char * string__uint64_t时间戳=实时();_ _ uint 64 _ t retvalint lenchar buf[128];如果(时间戳==0ULL){ RETURN _ FALSE;}自旋锁(锁定,PID);if(context-last _ timestamp==timestamp){ context-sequence=(context-sequence 1)context-sequence _ mask;if(context-sequence==0){ timestamp=skip _ next _ millis();} } else { context-sequence=0;/*回零*/}上下文-last_timestamp=时间戳;retval=((timestamp-context-twe poch)context-timestamp _ left _ shift)|(context-data center _ id context-data center _ id _ shift)|(worker _ id context-worker _ id _ shift)| context-sequence;spin_unlock(锁定,PID);//printf('%ld ',retval);srand((无符号)retval);//srand((无符号)时间(空));if((string=(char *)emalloc(length))==NULL){//myLog(' Malloc失败!标志:14 n ');RETURN _ NULL();} for(I=0;我长度-1;I){ flag=rand()% 3;switch(flag){ case 0: string[I]=' A ' rand()% 26;打破;case 1:字符串[I]=' a ' rand()% 26;打破;案例2:字符串[I]=' 0 ' rand()% 10;打破;默认值:字符串[I]=' x ';打破;} }字符串[length-1]=“0”;RETURN_STRINGL(字符串,长度,0);}

上图是服务器端编程语言(专业超文本预处理器的缩写)生成18W随机字符串所用的时间

上图是C扩展生成18W随机字符串所用的时间

所用的服务器都是第一代内存双核的阿里云服务器。

只要在数字证书中加入上如代码就可以生产随机字符串和随机长度数字字符串,PHP唯一身份生成扩展ukey。

php.ini的配置项:

ukey。数据中心=整数ukey。worker=整数ukey。twe poch=uint 64数据中心配置项是一个整数,用于设置数据中心;工人配置项是一个整数,用于设置数据中心的机器序号;twepoch配置项是一个64位的整数,用于设置时间戳基数,此值越大,生成的身份越小;

安装:

$ cd ./ukey$ phpize$ ./configure $ make $ sudo make install Ukey提供3个有用的函数:

ukey_next_id() -用于生成唯一标识ukey _ to _时间戳(标识)-用于将身份转换成时间戳ukey_to_machine(ID) -用于将身份转换成机器信息

使用实例:

?PHP $ id=ukey _ next _ id();echo $ id $ timestamp=ukey _ to _ timestamp($ id);回声日期(' Y-m-d H:i:s ',$ timestamp);$ info=ukey _ to _ machine($ id)var _ dump($ info);以上就是本文的全部内容,希望对大家的学习有所帮助。

更多资讯
游戏推荐
更多+