首頁  >  文章  >  後端開發  >  PHP 隨機數 C擴充隨機數

PHP 隨機數 C擴充隨機數

PHP中文网
PHP中文网轉載
2016-05-07 14:34:458086瀏覽

這篇文章跟大家介紹一下php 隨機數  c擴充隨機數。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

PHP 隨機數 C擴充隨機數

由於要使用到固定長度的隨機字串。

首先是一段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);

PHP 隨機數 C擴充隨機數

產生180000個隨機字串,圖中是依照重複數量倒序排列,可以看到基本上都有重複的。不過也是比較理想的。

由於想提升自己的c語言能力,所以用c重新寫了一下隨機產生字串。

其中用到了隨機數函數srand(),rand();

不過折騰一兩個小時,隨機數還是有問題。並發訪問時時間可能幾乎為同時,那麼srand給的種子時間可以視為相同的。這樣就導致了,產生的隨機數也是一樣的。從而產生的隨機字串也是一樣的。循環輸出隨機字串,幾乎都是一模一樣的。

後來想到了ukey,這個擴充可以實現唯一的id,那麼存取都產生唯一的id,是不是可以將這個id作為種子時間。答案是肯定的。

PHP 隨機數 C擴充隨機數

上圖是產生的隨機字串,可以自訂長度。也同樣可以輸出只有數字的字串。相較php所產生的隨機字串重複率更低且速度較快。

 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 隨機數 C擴充隨機數

上圖是php產生18w隨機字串所用的時間

PHP 隨機數 C擴充隨機數

上圖是c擴展生成18w隨機字串所用的時間

所用的伺服器都是1g記憶體雙核心的阿里雲伺服器。

只要在ukey中加入上如程式碼就可以生產隨機字串和隨機長度數字字串

ukey的位址http://www.oschina.net/p/ukey

推薦學習:《php影片教學

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除