yes , thinking of using a randomly generated number and then encoding it would be a great idea at first, but lets say a base62 encoded string of length 6 would have 54B combinations of strings but when we try to generate a random number and then encode it (saying 1000 rps) the collision rate is around 880k strings which can be an issue and require the service to double check the availability of the shortened string/url.
So, rather using a counter which avoids re checking the db for availability but has some security issues, and finally the bijectibve function does a one to one mapping using the id of the url in db and then base encoding and when retrieving it does inverse function to retrieve the long url, in both cases it l saves us time not checking the db for collision check.