Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    which one is better?

    Which one do you prefer among the two?
    PHP Code:
    function genRandomNo($length 5){    
        
    $string substr(md5(uniqid(rand(), true)), 0$length);
        return 
    $string;

    Vs
    PHP Code:
    function genRandomNo($length 5){    
        
    $string substr(md5(microtime() * mktime()), 0$length);
        return 
    $string;

    also you can suggest alternatives.

    Thanks

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Generate a random alphanumeric string of any length:
    PHP Code:
    function random_string$length )
    {
        return
            
    substr(
                
    str_shuffle(
                    
    str_repeat(
                        
    md5(
                            
    uniqid(
                                
    mt_rand(), TRUE
                            
    )
                        ), 
    ceil(
                            ( int ) 
    $length 32
                        
    )
                    )
                ), 
    0, ( int ) $length
            
    );

    What this does is generate an MD5 hash using uniqid()... but it repeats the string back to back to compensate for the requested length.

    For example, an MD5 hash is 32 characters in length. So if the requested $length is 37, it will take the ceiling of 37/32, which is 2. Multiply that by 32 and you have 64 characters... so you'll always have enough to work with.

    Then it shuffles that using str_shuffle() and reduces it using substr().

    Could be seen as an overkill, benchmarks consistently came in between 0.0001 and 0.0003 seconds.
    Last edited by kbluhm; 03-11-2010 at 02:27 PM.

  • #3
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by kbluhm View Post
    benchmarks consistently came in between 0.0001 and 0.0003 seconds.
    Versus?
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Versus nothing.

    .0001 isn't the fastest processing on planet earth, but try getting a random string of, ohhh let's say, 56 characters with those two original functions. Not happening.

    When you creep up into 5,000 characters, that's when you start seeing times between .0002 and .0004. 50,000 characters? Between .0018 and .0022.

    It demonstrates what I see as a fair trade-off between "typically-lengthed" random strings, but without the 32 character limit of an MD5 hash length.

    The first one by the OP was clocked at 2.0e-5, which is wayyy faster, but limited to 32 characters.

    If you will never need a random string of more than 32 characters, then either one of those would work. I'd prefer uniqid() over multiplying microtime() though. And if you continue to use rand(), consider swapping it out for mt_rand(), which is 4-times faster.
    Last edited by kbluhm; 03-11-2010 at 02:52 PM.

  • #5
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Thanks for the elaboration; very interesting
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •