In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to write PHP extended random numbers", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write PHP extended random numbers.
Because a random string of fixed length is used.
The first is a piece of PHP code.
$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 random strings are generated, and the figure is arranged in reverse order according to the number of repeats. You can see that there are basically duplicates. But it's also ideal.
Because I want to improve my C language ability, I rewrite the randomly generated string with C.
The random number functions srand () and rand () are used.
But after messing around for an hour or two, there is still a problem with random numbers. The time of concurrent access may be almost the same, so the seed time given by srand can be considered the same. As a result, the random numbers generated are the same. The resulting random string is the same. Loop output random strings, almost all exactly the same.
Later, I thought of ukey, this extension can implement a unique ID, so the access produces a unique ID, so whether this ID can be used as a seed time. The answer is yes.
The image above is a random string that can be customized in length. You can also output strings with only numbers. The random string repetition rate is lower and faster than the random string generated by 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) timestamp_left_shift) | (context- > datacenter_id datacenter_id_shift) | (worker_id 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 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 = timest retval = ((timestamp-context- > twepoch) timestamp_left_shift) | (context- > datacenter_id datacenter_id_shift) | (worker_id 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 failedbroken colors 14\ n"); RETURN_NULL ();} for (I = 0; I < length-1; iTunes +) {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] ='\ 0mm; RETURN_STRINGL (string,length,0);}
The picture above shows the time it took PHP to generate 18W random strings.
The figure above shows the time it takes for C extension to generate 18W random strings.
The servers used are all Ali cloud servers with 1G memory and dual cores.
Random strings and random length numeric strings can be produced as long as code such as this is added to ukey, and PHP unique ID generates extended ukey.
Configuration items for php.ini:
[ukey] ukey.datacenter = integerukey.worker = integerukey.twepoch = uint64
The datacenter configuration item is an integer used to set up the data center
The worker configuration item is an integer that sets the machine serial number of the data center
The twepoch configuration item is a 64-bit integer used to set the timestamp cardinality. The higher this value, the smaller the generated ID.
Installation:
$cd. / ukey$ phpize$. / configure$ make$ sudo make install
Ukey provides three useful functions:
Ukey_next_id ()-used to generate a unique ID
Ukey_to_timestamp (ID)-used to convert ID to timestamp
Ukey_to_machine (ID)-used to convert ID into machine information
Examples of use:
At this point, I believe you have a deeper understanding of "how to write PHP extended random numbers". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.