As long as the numbers are integers adding to 100 then probably the simplest way to do this is to create a list which has each number repeated that many times in it, and then take a random choice from that list.
import random
Thislist = [20, 34, 46]
l = [n for v in Thislist for n in [v] * v]
print(random.choice(l))
Example test code:
res = {}
for _ in range(1000000):
c = random.choice(l)
res[c] = res.get(c, 0) + 1
print(res)
Sample output:
{46: 459771, 20: 200242, 34: 339987}
A note about performance.
There is obviously a setup cost here in forming the list l
. If there are only to be a few selections from made from the list, @TharunK's answer is more efficient. However beyond that small number, random.choice
is enough faster (~4x from my testing) than random.choices
to make this solution far more efficient.