因爲要用到固定長度的隨機字符串。php
首先是一段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個隨機字符串,圖中是按照重複數量倒序排列,能夠看到基本都有重複的。不過也是比較理想的。併發
因爲想提高一下本身的C語言能力,因此用C從新寫了一下隨機生成字符串。dom
其中用到了隨機數函數srand(),rand();函數
不過折騰一兩個小時,隨機數仍是有問題。併發訪問時時間可能幾乎爲同時,那麼srand給的種子時間能夠視爲相同的。這樣就致使了,產生的隨機數也是同樣的。從而產生的隨機字符串也是同樣的。循環輸出隨機字符串,幾乎都是如出一轍的。ui
後來想到了ukey,這個擴展能夠實現惟一的ID,那麼訪問都產生惟一的ID,是否是能夠將這個ID做爲種子時間。答案是確定的。阿里雲
上圖是產生的隨機字符串,能夠自定義長度。也一樣能夠輸出只有數字的字符串。相較PHP所產生的隨機字符串重複率更低且速度更快。.net
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隨機字符串所用的時間code
上圖是C擴展生成18W隨機字符串所用的時間ip
所用的服務器都是1G內存 雙核的阿里雲服務器。
只要在ukey中加入上如代碼就能夠生產隨機字符串和隨機長度數字字符串
ukey的地址http://www.oschina.net/p/ukey