note 59695 deleted from function.rand by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:44:26 +0000 |
| Subject: | note 59695 deleted from function.rand by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134434@lists.php.net to get a copy of this message | ||
Note Submitter: RichardBronosky (1st at last dot com)
----
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)) );
?>