note 59695 added to function.rand

From: Date: Tue, 13 Dec 2005 18:40:45 +0000
Subject: note 59695 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-100080@lists.php.net to get a copy of this message
I like the idea of expertphp at y!, but the implementation was too expensive for my load. Here is the tightest solution to generating $x random [[:alnum:]] chars that I could think of. I use this for creating random file names. <?php // Assuming $z is "_file_prefix_" or an empty string. // If $z is unset an E_NOTICE will be added to your logs. // That's bad for production boxes! for($x=0;$x<32;$x++){ $y = rand(0,61); $z .= chr( $y + (($y<10) ? 48 : (($y<36) ? 55 : 61)) ); } echo $z; ?> To get an array, just change this line: <?php // $z can be unset, null, or an existing array. E_ERROR if it's a string. $z[] = chr( $y + (($y<10) ? 48 : (($y<36) ? 55 : 61)) ); ?> ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59695 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59695&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59695&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100080) next »