How do you create unique "friend code"s?

There are many games that generate 9-digit "friend code"s which are used for referrals and creating alliances. I want to know how to do this in an unpredictable way. Any ideas??

Thanks in advance!

Well, i guess you use some sort of global database on your website which holds all the users in a table, right? If not how would you actually “identify” a user?

So in your DB table you usually have an auto increment ID which is already unique. All you have to do is turn that number into an “unpredictable number” by using something like this:

It actually does the same as base64 but use base62 and does some scrambling (based on a secret salt) on the actual alphabet so without knowing the salt it’s impossible to derive the actual ID from the encoded string.