I'm currently working on a project that I will be generating random identifiers that contain letters and numbers. It will be all uppercase. I'm trying to figure out the best solution to generate them very randomly so it's less likely they will collide. I will of course be checking to see if the identifier is already in use, and if it is regenerate, but I'd prefer to have to do the least amount of checks as possible.
I was thinking that maybe hashing data from urandom and using substr() to take X amount of characters off the hash. Anyone else have any other better ideas?
Example...
Code: Select all
function generateTransactionID( )
{
do {
if ( ( $handle = fopen( '/dev/urandom', 'rb' ) ) ) {
$random = fread( $handle, 8 );
fclose( $handle );
}
$hash = substr( md5( $random ), 0, 12 );
// $transaction = query DB if found true else false
} while ( $transaction !== false );
return strtoupper( $hash );
}
Thanks93C88EE7E607
4DFD36F7A366
5D20CF60E253
5796307D51EB
67BEE2A79FDF
9D4C58E7B8EB
3C6183CED554
DFDDF00A1289
62DE6F4B0FA9
1510EECCBFDC