A method for generating random uniqid strings using the Mt_rand in PHP

Source: Internet
Author: User
Tags 04x md5 rand sha1 sprintf

Method One:

?     PHP
$str = uniqid (Mt_rand (), 1);
Echo ' SHA1: ', SHA1 ($STR);
Echo ' <br> ';
Echo ' MD5: ', MD5 ($STR);


Using Uniqid function, with Mt_rand random function, finally using hash is worth the final unique value.


But in the test, the uniqueness of the random number was found to be somewhat the same, with approximately 11 identical (but no serious)

Second method: GUID

? PHP        echo sprintf ('%04x%04x-%04x-%04x-%04x-%04x%04x%04x ',  mt_rand ( 0, 65535),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (16384, 20479),  mt_rand (32768, 49151),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (0,

 65535));

     echo  "<br>";      echo sprintf ('%04x%04x-%04x-%04x-%04x-%04x%04x%04x ',  mt_rand (0,  65535),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (16384, 20479),  mt_ Rand (32768, 49151),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (0, 65535)

);

     echo  "<br>";      echo sprintf ('%04x%04x-%04x-%04x-%04x-%04x%04x%04x ',  mt_rand (0,  65535),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (16384, 20479),   Mt_rand (32768, 49151),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (0, 

65535));

     echo  "<br>";      echo sprintf ('%04x%04x-%04x-%04x-%04x-%04x%04x%04x ',  mt_rand (0,  65535),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (16384, 20479),  mt_ Rand (32768, 49151),  mt_rand (0, 65535),  mt_rand (0, 65535),  mt_rand (0, 65535)

);    ?>


Example 3

<?php
//using uniqid Mt_rand to generate random, repetitive strings
$snKeys =array ();
$total =10;//Set the build quantity
$prefix = ' code ';//String prefix while
(count ($snKeys) < $total) {
$snKeys [$prefix. MD5 ( Uniqid (Mt_rand (), true)] = null;
}
Print_r ($snKeys);


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.