Take the 2-minute tour ×

Recently I found a function to generate a random string of numbers and letters, and It was really compact and concise and I liked it alot. The problem Is I have no idea what it is doing, or how to modify it.

Please can someone just explain to me step by step what is going on here. I have tried looking up the functions alone but they don't seem to explain the things like 0x19A100 etc.

I would like to modify it to produce the same random string but only around 4 or 5 lettes long. I don't just want to take the first few letters though, I wan't the function to properly produce. I am trying to learn!

Here is the code:

function genRndStr() 
    {   return strtoupper(base_convert(mt_rand(0x19A100, 0x39AA3FF), 35, 36));  }

Any help greatly appreciated! Thank you.

share|improve this question
add comment

2 Answers

mt_rand generates random numbers between (min, max). The numbers 0x19A100, 0x39AA3FF are simply the min and max values in hexadecimal format:

0x19A100 = 1 679 616
0x39AA3FF = 60 466 175
share|improve this answer
so why not just write the numbers as they are? –  Chud37 Jan 30 '13 at 11:24
And why use such large numbers? –  Chud37 Jan 30 '13 at 11:25
@Chud37 Both your questions can only be answered by whoever wrote that piece of code, we can only speculate. –  Yannis Rizos Jan 30 '13 at 11:27
I think is because base_convert take hexadecimal number as first attribute and large number is there to make generated string longer. ASCII characters are encoded in 3 digits decimal number, so every 3 numbers make one character in string. Bigger number == longer string. –  tikend Jan 30 '13 at 11:28
@tikend base_convert can take any number in any base system as its first argument not just hexadecimals. That's kind the point of the function, converting numbers from one base (any base) to another. –  Yannis Rizos Jan 30 '13 at 11:33
show 4 more comments

As tikend mentioned

mt_rand takes min and max values and generates a random value from that range.

Then that output from mt_rand is given to base_convert where the random value is assumed to be BASE 35 and it is converted to BASE 36 system.

The output of base_convert is then converted to uppercase.

share|improve this answer
Where can I learn about these base systems? I have never heard of base 36 before but it looks to be quite useful –  Chud37 Jan 30 '13 at 11:28
I still don't see the need for such ridiculously large numbers though? –  Chud37 Jan 30 '13 at 11:30
en.wikipedia.org/wiki/Base_36 –  Vivekanand S V Jan 30 '13 at 11:31
@Chud37 It will be useful when u are calculating something like largest prime number, etc., where base 36 system will simplify the number which takes lot of space to show in BASE 10 system, etc –  Vivekanand S V Jan 30 '13 at 11:33
@VivekanandSV - I would say that technically it would take up the same amount of memory, the reason for using say Base 16/36 is for the human element, instead of having to write 1,679,616 in binary which would be a 64-bit binary value it can be displayed in a much more compact way. Unless of course space means something other then memory in the context your using it in? –  Ramhound Jan 30 '13 at 14:02
show 1 more comment

Your Answer


