From 9e2df7c1fc8f8f5c9837f52c308f72e9f326b577 Mon Sep 17 00:00:00 2001 From: druu Date: Tue, 6 Sep 2011 22:19:59 +0300 Subject: Major speed improvement in function random_string() for cases 'alpha', 'alnum', 'numeric' and 'nozero' --- system/helpers/string_helper.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'system/helpers/string_helper.php') diff --git a/system/helpers/string_helper.php b/system/helpers/string_helper.php index 9fa69f46c..dd8ffaddb 100644 --- a/system/helpers/string_helper.php +++ b/system/helpers/string_helper.php @@ -215,12 +215,9 @@ if ( ! function_exists('random_string')) case 'nozero' : $pool = '123456789'; break; } - - $str = ''; - for ($i=0; $i < $len; $i++) - { - $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1); - } + + $str = substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len); + return $str; break; case 'unique' : -- cgit v1.2.3-24-g4f1b