PHP隨機(jī)數(shù) C擴(kuò)展隨機(jī)數(shù)
由于要用到固定長(zhǎng)度的隨機(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ù)的。不過(guò)也是比較理想的。
由于想提升一下自己的C語(yǔ)言能力,所以用C重新寫了一下隨機(jī)生成字符串。
其中用到了隨機(jī)數(shù)函數(shù)srand(),rand();
不過(guò)折騰一兩個(gè)小時(shí),隨機(jī)數(shù)還是有問(wèn)題。并發(fā)訪問(wèn)時(shí)時(shí)間可能幾乎為同時(shí),那么srand給的種子時(shí)間可以視為相同的。這樣就導(dǎo)致了,產(chǎn)生的隨機(jī)數(shù)也是一樣的。從而產(chǎn)生的隨機(jī)字符串也是一樣的。循環(huán)輸出隨機(jī)字符串,幾乎都是一模一樣的。
后來(lái)想到了ukey,這個(gè)擴(kuò)展可以實(shí)現(xiàn)唯一的ID,那么訪問(wèn)都產(chǎn)生唯一的ID,是不是可以將這個(gè)ID作為種子時(shí)間。答案是肯定的。

上圖是產(chǎn)生的隨機(jī)字符串,可以自定義長(zhǎng)度。也同樣可以輸出只有數(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ī)長(zhǎng)度數(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);
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- 深入理解PHP中mt_rand()隨機(jī)數(shù)的安全
- PHP簡(jiǎn)單獲取隨機(jī)數(shù)的常用方法小結(jié)
- PHP獲取redis里不存在的6位隨機(jī)數(shù)應(yīng)用示例【設(shè)置24小時(shí)過(guò)時(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簡(jiǎn)單生成隨機(jī)數(shù)的方法
- php源碼分析之DZX1.5隨機(jī)數(shù)函數(shù)random用法
- PHP的偽隨機(jī)數(shù)與真隨機(jī)數(shù)詳解
- PHP生成隨機(jī)數(shù)的方法總結(jié)
相關(guān)文章
php頁(yè)碼形式分頁(yè)函數(shù)支持靜態(tài)化地址及ajax分頁(yè)
這篇文章主要介紹了php頁(yè)碼形式分頁(yè)函數(shù),此分頁(yè)支持靜態(tài)化地址分頁(yè)和無(wú)鏈接地址時(shí)的ajax分頁(yè),需要的朋友可以參考下2014-03-03
XAMPP升級(jí)PHP版本實(shí)現(xiàn)步驟解析
這篇文章主要介紹了XAMPP升級(jí)PHP版本實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
PHP關(guān)鍵字Self、Static和parent的區(qū)別詳解
在使用PHP代碼時(shí),您可能經(jīng)常會(huì)遇到parent::、static::和self::,但是當(dāng)你第一次作為一個(gè)開發(fā)人員開始的時(shí)候,有時(shí)候你會(huì)很困惑,不知道它們是做什么的,以及它們之間的區(qū)別,本文給大家介紹了PHP關(guān)鍵字Self、Static和parent的區(qū)別,需要的朋友可以參考下2024-12-12
PHP實(shí)現(xiàn)的猴王算法(猴子選大王)示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的猴王算法(猴子選大王),對(duì)比分析了兩種PHP猴王算法,涉及PHP數(shù)組遍歷、判斷、遞歸等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
PHP 命名空間和自動(dòng)加載原理與用法實(shí)例分析
這篇文章主要介紹了PHP 命名空間和自動(dòng)加載,結(jié)合實(shí)例形式分析了PHP 命名空間和自動(dòng)加載具體功能、概念、原理與使用技巧,需要的朋友可以參考下2020-04-04
php實(shí)現(xiàn)XSS安全過(guò)濾的方法
這篇文章主要介紹了php實(shí)現(xiàn)XSS安全過(guò)濾的方法,實(shí)例分析了php針對(duì)XSS進(jìn)行安全過(guò)濾的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
PHP中strtr與str_replace函數(shù)運(yùn)行性能簡(jiǎn)單測(cè)試示例
這篇文章主要介紹了PHP中strtr與str_replace函數(shù)運(yùn)行性能簡(jiǎn)單測(cè)試,結(jié)合具體實(shí)例形式對(duì)比分析了PHP中strtr與str_replace函數(shù)的測(cè)試運(yùn)行效率,需要的朋友可以參考下2019-06-06

