Quick way to generate something random

Thought I’d write a quick post to pass the time. I’m currently working on a little pet project that involves PHP, MySQL and is supposed to be a database of things that can be added dynamically. I did write it in a ‘procedural loop’ form but scrapped it due to its unmaintainability and the fact that object orientation is just so much better.
Those who know me and where I frequent online, will know what this project is; and will be released… Eventually. :)

One thing I did need was a little section of code that would generate a ‘random’ (when it comes to computers, nothing can be truely random) value to use as a primary key in a database, therefore making the entry unique across the entire database. This was required to link information across tables and the values generated by this code snippet would form primary and foreign keys. MySQL doesn’t seem to have such random generation functionality (that I could find anyway) so PHP code was the next best thing.

I found a code snippet that was based around the same thing posted below, but was designed as a session key generator (and had quite a bit more code to it. When modified, however, met my needs more than adequately.

function guid()
    $id  =  md5(microtime());
    return $id;

Quick explanation: I’ve enclosed this as a function so that I can re-use it where-ever I need some sort of random value. microtime() is a PHP class that returns the time in seconds since the Unix Epoch, and md5() just puts whatever value that is enclosed as an MD5 hash, making it a consistent hexadecimal 32-character value. There are probably better ways of doing this, but this works; which works for me. The return statement you see allows me to write code not dissimilar to this (if the function is in the same script):


to generate the random value, which is then posted to the database under the relevant record.

Someone may find this of use, so here it is. Object orientation is difficult in PHP but once you get the hang of it, it’s quite intuitive; if a little confusing at times.

This entry was posted in Technical. Bookmark the permalink.

2 Responses to Quick way to generate something random

  1. Tim says:

    Depending on how many rows you are recording, once you get to hundreds of thousands / millions of rows, the MD5 hash, due to the birthday paradox, is insufficiently guaranteed to provide uniqueness, though if there’s a SHA1 or SHA256 you should be okay for a lot longer.

    Incidently, because collisions are more likely in MD5 than would mathematically seem likely (http://en.wikipedia.org/wiki/MD5#Security) they are less secure when used as a one-way hash, if, for example you where to hash student ID’s for wider dissemination.

    • admin says:

      Yeah, there always was that risk; that said this particular project (which has since been indefinitely placed on the back burner) was never going to have that many rows. It’s definitely good to keep that in mind for the future!

      Cheers for the tip!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>