21 million was not chosen, 21 million is the result of multiple other choices.
Initial block reward was 50BTC
1 block is made every 10 minutes
Block reward halves every 4 years
Those three choices lead to 21 million coins, after so many halvings the block reward eventually hits 0 and at that time the number of coins generated happens to be just under 21 million.
if you look into the source code, 21m was hardcoded, not derived as you have mentioned
Oh, where is that?
Only thing that's hardcoded is:
1. the initial 50 BTC per block
2. reward halving every 210000 blocks
Just these two factors implicitly determine the 21M limit (or actually: 20999999.9769 BTC).