From 517935bfabd42051bf99de31716d72da0f5c0379 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 22 Apr 2007 06:15:28 +0000 Subject: [PATCH] Optimizing BSFunctions::Random() ever so slightly so we only need one loop instead of many --- Functions.php | 60 ++++++++++++++------------------------------------- 1 file changed, 16 insertions(+), 44 deletions(-) diff --git a/Functions.php b/Functions.php index 340b9e9..27a4b37 100644 --- a/Functions.php +++ b/Functions.php @@ -410,56 +410,28 @@ class BSFunctions } else if (!$length) { - // Gimme a length! $length = rand(20, 65); } - // Number of ints in our salt - $intcount = rand(1, intval($length / 2)); - - // Number of chars - $charcount = $length - $intcount; - - // Upper-case chars - $upperchars = rand(1, intval($charcount / 2)); - - // Lower-case chars - $lowerchars = $charcount - $upperchars; - - // Generate ints - for ($i = 0; $i < $intcount; $i++) - { - $string[ mt_rand() ] = rand(0, 9); - } - - // Generate upper chars - for ($i = 0; $i < $upperchars; $i++) - { - $string[ mt_rand() ] = chr(rand(65, 90)); - } - - // Generate lower chars - for ($i = 0; $i < $lowerchars; $i++) - { - $string[ mt_rand() ] = chr(rand(97, 122)); - } - - if ($length != sizeof($string)) - { - return $this->rand($length); - } - - // Sort the chars by thier random assignment - ksort($string); - - // Flatten the array - $return = ''; - foreach ($string AS $char) + $string = ''; + while (strlen($string) < $length) { - $return .= $char; + $type = rand(0, 300); + if ($type < 100) + { + $string .= rand(0, 9); + } + else if ($type < 200) + { + $string .= chr(rand(65, 90)); + } + else + { + $string .= chr(rand(97, 122)); + } } - return $return; + return $string; } // ################################################################### -- 2.22.5