find random country but probability of picking higher population country should be higher [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
Random number with Probabilities
10 answers
I am working on below interview question: If you're given a list of countries and its corresponding population, write a function that will return a random country but the higher the population of the country, the more likely it is to be picked at random. I came up with below logic: Make two traversal on the list. In the first traversal, sum up all the population of every location to get the total population TOTAL_POP. In the second iteration, calculate % of each location's population against TOTAL_POP. For example, if location A has a popoulation 'a'. The percentage of