tags:

views:

85

answers:

4

Hi, what is the fastest way to generate a random two long alphanumeric character?

Thanks, Max

A: 

How about

$az = range('a', 'z');
$r = array_rand($az, 2);
$str = $az[$r[0]] . $az[$r[1]];
Jani Hartikainen
+3  A: 

This would do the job beautifully:

substr(md5(uniqid(microtime(true),true)),0,2);

This technique is used quite often for alphanumeric password salts in databases.

brianreavis
I like this approach.
mistero
No G-Z, though!
Artelius
Better than mine. =)
Jani Hartikainen
Artelius has a valid point. I'm not sure if that's an issue for your application or not, though.
brianreavis
A: 

Based on Jani's answer:

join('', array_rand(array_merge(range('a', 'z'), range('0', '9')), 2))
Artelius
A: 

how about this?

base_convert(rand(0,35), 10, 36) . base_convert(rand(0,35), 10, 36)
gahooa