note 32982 deleted from function.rand by aidan

From: Date: Sat, 11 Sep 2004 07:42:48 +0000
Subject: note 32982 deleted from function.rand by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-76453@lists.php.net to get a copy of this message
Note Submitter: scott at rocketpack dot net ---- [ Editor's Note: The string which this function ultimately creates may and probably will repeat given sufficient itterations even though the seed value is based on the time which is a sequentially increasing number. The fequency of duplication is dependent on $length but can, theoretically, happen over a very short cycle. ] If you want a REALLY random string, here's how you do it: function randStr( $length ) { if ( $length > 36 ) { return "ERROR"; } else { $str = md5(mktime()); $cutoff = 31 - $length; $start = rand(0, $cutoff); return substr($str, $start, $length); } } Not only does it first generate a number that will never repeat, but it then MD5s it for flavour, then it chops it down to whatever length you want (up to 36 characters). However, if you do not use the full 36, it will chop it from a random place in the string, ie: e5hdksuth834h5hfcb6j4la43gj7e0p234b6 is generated... if you specify 5 chars, it wont just use: e5hdk or 234b6 (first / last 5 chars of the string), but it will use a string of 5 chars from any randomly selected location (within the necessary scope) within the string! Pretty nifty, eh'? -my 2¢

« previous php.notes (#76453) next »