If your friendly marketer asks you to implement purchase IDs or to serialise events, you’ll have to generate 20-character IDs that should be as unique as possible.
The straight-forward approach is to use some existing IDs that the back-end provides, but often those can be longer than 20 characters.
So what can you do?
The obvious answer is to use a hash function, ideally a perfect one.
But even those hash functions return more than 20 characters, right? Even a simple SHA-1 represented in Base64 binary is longer.
The thing is, though, that With a good hash function, the unpredictability of any part of the hash is proportional to the part’s size. (see this article on stackoverflow). In other words, we can use any hash function we like and then just truncate the result to 20 characters.