PHP random number C Extended Random number _php technique

Source: Internet
Author: User
Tags echo date rand ukey unique id

Because you want to use a fixed-length random string.

First is a section of PHP code

$str _md5=md5 (Uniqid ());
  $rand = Mt_rand (1);
  $str 1=substr ($str _md5, $rand, 6);
  $rand = Mt_rand (1);
  $str 2=substr ($str _md5, $rand, 6);
  $rand = Mt_rand (1);
  $str 3=substr ($str _md5, $rand, 6);
  $code =substr ($str 1 $str 2 $str 3,0,8);


Generates 180,000 random strings, which are sorted in reverse order, and can be seen as having duplicates. But it's also more ideal.

Because I wanted to improve my C language ability, I wrote a random generation string with C again.

which uses the random number function Srand (), Rand ();

But for one or two hours, there's still a problem with random numbers. Concurrent access time may be almost simultaneous, so the seed time Srand can be considered the same. This leads to the same random number that is produced. The resulting random string is also the same. The loop outputs random strings, almost all identical.

Then came the thought of Ukey, which can implement a unique ID, so access has a unique ID, and it is not possible to take this ID as the seed time. The answer is yes.

The image above is a random string generated and can be customized to a length. You can also output strings that have numbers only. Compared to PHP, the random string repetition rate is lower and faster.

 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->seq
    Uence_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] = ' I ';
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->seq
    Uence_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] = ' I ';
Return_stringl (string,length,0);

 }


The image above is the time that PHP takes to generate a 18W random string

The above illustration is the time spent in C extension to generate a 18W random string

The servers used are both 1G memory dual-core Aliyun servers.

The PHP unique ID generates an extension ukey as long as the code is added to the ukey to produce random strings and random-length numeric strings.

Configuration items for php.ini:

[Ukey]
Ukey.datacenter = integer
ukey.worker = integer
Ukey.twepoch = UInt64

The Datacenter configuration entry is an integer that is used to set up the data center;
A worker configuration entry is an integer that is used to set the machine number of the data center;
The Twepoch configuration entry is a 64-bit integer that sets the timestamp base, with the larger the value, the smaller the generated ID;

Installation:

$ CD./ukey
$ phpize $
/configure $
Make

Ukey provides 3 useful functions:

UKEY_NEXT_ID ()--for generating unique IDs
Ukey_to_timestamp (ID)--for converting IDs to timestamps
Ukey_to_machine (ID)--for converting IDs to machine information

Use instance:

<?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);
>

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.