Free Download Mozila fire Fox

Thursday, July 19, 2007

Random String Generator

This is one of my smallest, most powerful, most widely used function. It takes the input bewteen 1-32 and will return the string of that particular length. The string that is generated is completely random, as is the way it is selected within the function.
The beauty of this is that if a length less than 32 is wanted, it will randomly select the position as well.
< ?

function randomString($length)
{
// Generate random 32 charecter string
$string = md5(time());

// Position Limiting
$highest_startpoint = 32-$length;

// Take a random starting point in the randomly
// Generated String, not going any higher then $highest_startpoint
$randomString = substr($string,rand(0,$highest_startpoint),$length);

return $randomString;

}

?>

To use the function:

< ?php

// Gets a random string of length 10
$randomString = randomString(10);

?>

This function literally has a statistical chance of repeating the same value next to never. Unless, of course, you only want 1 charecter returned. At that point, their are only 34 different possibilities that it could be (a-z and 0-9). Other then that, it will be pretty unique!

Thats about it! It is pretty self explanitory. Enjoy!

No comments: