PHP隨機(jī)數(shù) C擴(kuò)展隨機(jī)數(shù)
由于要用到固定長度的隨機(jī)字符串。
首先是一段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);
生成180000個(gè)隨機(jī)字符串,圖中是按照重復(fù)數(shù)量倒序排列,可以看到基本都有重復(fù)的。不過也是比較理想的。
由于想提升一下自己的C語言能力,所以用C重新寫了一下隨機(jī)生成字符串。
其中用到了隨機(jī)數(shù)函數(shù)srand(),rand();
不過折騰一兩個(gè)小時(shí),隨機(jī)數(shù)還是有問題。并發(fā)訪問時(shí)時(shí)間可能幾乎為同時(shí),那么srand給的種子時(shí)間可以視為相同的。這樣就導(dǎo)致了,產(chǎn)生的隨機(jī)數(shù)也是一樣的。從而產(chǎn)生的隨機(jī)字符串也是一樣的。循環(huán)輸出隨機(jī)字符串,幾乎都是一模一樣的。
后來想到了ukey,這個(gè)擴(kuò)展可以實(shí)現(xiàn)唯一的ID,那么訪問都產(chǎn)生唯一的ID,是不是可以將這個(gè)ID作為種子時(shí)間。答案是肯定的。
上圖是產(chǎn)生的隨機(jī)字符串,可以自定義長度。也同樣可以輸出只有數(shù)字的字符串。相較PHP所產(chǎn)生的隨機(jī)字符串重復(fù)率更低且速度更快。
PHP_FUNCTION(get_random__num_str) { int length=8; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE) { length=8; } length++; int flag, i; char* string; __uint64_t timestamp = realtime(); __uint64_t retval; int len; char buf[128]; if (timestamp == 0ULL) { RETURN_FALSE; } spin_lock(lock, 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; /* Back to zero */ } context->last_timestamp = timestamp; retval = ((timestamp - context->twepoch) << context->timestamp_left_shift) | (context->datacenter_id << context->datacenter_id_shift) | (worker_id << context->worker_id_shift) | context->sequence; spin_unlock(lock, pid); //printf('%ld',retval); srand((unsigned)retval); //srand((unsigned) time(NULL )); if ((string = (char*) emalloc(length)) == NULL ) { //myLog("Malloc failed!flag:14\n"); RETURN_NULL() ; } for (i = 0; i < length - 1; i++) { flag = rand() % 3; switch (flag) { case 0: string[i] = '1' + rand() % 5; break; case 1: string[i] = '2' + rand() % 7; break; case 2: string[i] = '0' + rand() % 10; break; default: string[i] = '9'; break; } } string[length - 1] = '\0'; RETURN_STRINGL(string,length,0); } PHP_FUNCTION(get_random_str) { int length=8; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE) { length=8; } length++; int flag, i; char* string; __uint64_t timestamp = realtime(); __uint64_t retval; int len; char buf[128]; if (timestamp == 0ULL) { RETURN_FALSE; } spin_lock(lock, 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; /* Back to zero */ } context->last_timestamp = timestamp; retval = ((timestamp - context->twepoch) << context->timestamp_left_shift) | (context->datacenter_id << context->datacenter_id_shift) | (worker_id << context->worker_id_shift) | context->sequence; spin_unlock(lock, pid); //printf('%ld',retval); srand((unsigned)retval); //srand((unsigned) time(NULL )); if ((string = (char*) emalloc(length)) == NULL ) { //myLog("Malloc failed!flag:14\n"); RETURN_NULL() ; } for (i = 0; i < length - 1; i++) { flag = rand() % 3; switch (flag) { case 0: string[i] = 'A' + rand() % 26; break; case 1: string[i] = 'a' + rand() % 26; break; case 2: string[i] = '0' + rand() % 10; break; default: string[i] = 'x'; break; } } string[length - 1] = '\0'; RETURN_STRINGL(string,length,0); }
上圖是PHP生成18W隨機(jī)字符串所用的時(shí)間
上圖是C擴(kuò)展生成18W隨機(jī)字符串所用的時(shí)間
所用的服務(wù)器都是1G內(nèi)存 雙核的阿里云服務(wù)器。
只要在ukey中加入上如代碼就可以生產(chǎn)隨機(jī)字符串和隨機(jī)長度數(shù)字字符串,PHP唯一ID生成擴(kuò)展ukey。
php.ini的配置項(xiàng):
[ukey] ukey.datacenter = integer ukey.worker = integer ukey.twepoch = uint64
datacenter配置項(xiàng)是一個(gè)整數(shù), 用于設(shè)置數(shù)據(jù)中心;
worker配置項(xiàng)是一個(gè)整數(shù), 用于設(shè)置數(shù)據(jù)中心的機(jī)器序號(hào);
twepoch配置項(xiàng)是一個(gè)64位的整數(shù), 用于設(shè)置時(shí)間戳基數(shù), 此值越大, 生成的ID越小;
安裝:
$ cd ./ukey $ phpize $ ./configure $ make $ sudo make install
Ukey提供3個(gè)有用的函數(shù):
ukey_next_id() -- 用于生成唯一ID
ukey_to_timestamp(ID) -- 用于將ID轉(zhuǎn)換成時(shí)間戳
ukey_to_machine(ID) -- 用于將ID轉(zhuǎn)換成機(jī)器信息
使用實(shí)例:
<?php $id = ukey_next_id(); echo $id; $timestamp = ukey_to_timestamp($id); echo date('Y-m-d H:i:s', $timestamp); $info = ukey_to_machine($id) var_dump($info); ?>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- 深入理解PHP中mt_rand()隨機(jī)數(shù)的安全
- PHP簡單獲取隨機(jī)數(shù)的常用方法小結(jié)
- PHP獲取redis里不存在的6位隨機(jī)數(shù)應(yīng)用示例【設(shè)置24小時(shí)過時(shí)】
- PHP基于自增數(shù)據(jù)如何生成不重復(fù)的隨機(jī)數(shù)示例
- php 指定范圍內(nèi)多個(gè)隨機(jī)數(shù)代碼實(shí)例
- php獲取一定范圍內(nèi)取N個(gè)不重復(fù)的隨機(jī)數(shù)
- php 利用array_slice函數(shù)獲取隨機(jī)數(shù)組或前幾條數(shù)據(jù)
- php簡單生成隨機(jī)數(shù)的方法
- php源碼分析之DZX1.5隨機(jī)數(shù)函數(shù)random用法
- PHP的偽隨機(jī)數(shù)與真隨機(jī)數(shù)詳解
- PHP生成隨機(jī)數(shù)的方法總結(jié)
相關(guān)文章
PHP5中使用DOM控制XML實(shí)現(xiàn)代碼
PHP5中增強(qiáng)了XML的支持,使用DOM擴(kuò)展了XML操作的能耐。這些函數(shù)作為 PHP5 核心的一部分,無需被安裝即可使用。2010-05-05