Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of different strings I can get using PHP random string shuffle

I currently run a URL shortening website called http://nn.pe and use a 6 char random hash with this code,

$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    return substr(str_shuffle($charset), 0, 6);

I was wondering how many different strings are there using those characters and integers?

You don't need to be exact, just within a million I guess.

like image 761
HarryBeasant Avatar asked Dec 27 '22 08:12

HarryBeasant


1 Answers

Warning

I just looked it up in the PHP 5.3.8 source. php_rand() is used. This is the C equivalent of rand(). I'd be very careful with str_shuffle!! It will (usually) give you as many possibilities as getrandmax() outputs, which can be 32,768 on Windows.

On most systems you'll have a theoretical maximum of 2.1 billion though.

like image 121
Tom van der Woerdt Avatar answered Jan 17 '23 17:01

Tom van der Woerdt