There is another way which is even simpler than my last approach. Since random.random() generates number between 0 to 1. If we square it, it will getting smaller. Hence, the lower value will have more chance.
def rand2(n):
r = random.random()
r = r * r
return round(r * n)
The result of rand(5) after 100,000 attempts
0 = 0.31604 (31%)
1 = 0.2316 (23%)
2 = 0.15841 (15%)
3 = 0.12994 (12%)
4 = 0.11245 (11%)
5 = 0.05156 ( 5%)
If you want to increase the chance of the lower value even further you can do r = r * r * r
. The result of rand(5) after 100,000 attempts
0 = 0.46415 (46%)
1 = 0.20534 (20%)
2 = 0.12483 (12%)
3 = 0.093 ( 9%)
4 = 0.07767 ( 7%)
5 = 0.03501 ( 3%)