note 32982 added to function.rand

From: Date: Thu, 12 Jun 2003 22:00:44 +0000
Subject: note 32982 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-50252@lists.php.net to get a copy of this message
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¢ ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32982 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32982&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32982&report=yes

« previous php.notes (#50252) next »