www.webdeveloper.com
Results 1 to 4 of 4

Thread: help with random code generation

  1. #1
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25

    help with random code generation

    Hi,

    I'm trying to generate a random reference code for an auction style website.

    Currently I'm using the following which generates a random ten character code.

    *This is not my code i found it elsewhere on the net.

    Code:
    $random = substr(base64_encode(rand(1000000,9999999)),0,7);
    This is fine but what I really need is do is add 'DAD' before each random seven character code.

    So in other words my reference code would be ten characters long
    ie
    DAD1234567

    Just wondering if anybody had any suggestions off the top of their heads.

    Many Thanks!

  2. #2
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25
    Just figured it out

    Code:
    $random = 'DAD' . substr(base64_encode(rand(1000000,9999999)),0,7);
    Cheers

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,610
    I don't know if this is an issue for what you are doing or not, but note that rand() only promises to generate a (pseudo) random number. It is not necessarily unique, so there is always a possibility that any value it generates will be a duplicate of a previously generated one. If this is an issue, you might look into using uniqid().
    PHP Code:
    $random "DDD" md5(uniqid('')); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25
    Hi Nog Dog,

    Thanks for pointing this out to me.

    I changed it and it worked fine.

    Thanks for your help again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles