Answer the question
In order to leave comments, you need to log in
Random algorithm in which the larger the number, the lower the chance of it falling out?
There is some variable - the wear of a thing, which is a number with two digits after the decimal point and a maximum value equal to: 99.99, how to make it random so that the higher this number, the lower the chance of falling out.
I did it through random ranges:
random(0,1000)
if the result is from 0 to 500 then randomly from 0 to 70
0 - 500 -> random(0, 70) and so on, but this is not very suitable, since the chance of falling out, for example, 90 and 99 are too equal, and writing each number is too long
Answer the question
In order to leave comments, you need to log in
You random.choices
have the ability to set the "weight" for the elements. For example:
import random
item_chances = {
'item_1': 10,
'item_2': 30,
'item_3': 50,
'item_4': 70,
'item_5': 90
}
selected = random.choices(
list(item_chances.keys()), weights=list(item_chances.values()), k=5000)
for item in set(selected):
print(f'{item}: {selected.count(item)}')
item_1
the smallest weight, that is, the smallest chance of getting this value, while y has item_5
the highest. The argument k
specifies how many elements to select. In this case, I chose 5000 for the test. Conclusion:item_1: 175
item_2: 578
item_3: 1001
item_4: 1458
item_5: 1788
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question