#percentage in random module
26 messages · Page 1 of 1 (latest)
Gui 0.5%
Schmidt 0.5%
Peido 1%
Thigas 1%
Mirato 3%
JM 7.5%
Guto 10%
Anselmo 30%
Emanuel 31.3%
Gui TOTS 0.1%
Schmidt TOTS 0.1%
Peido TOTS 0.25%
Thigas TOTS 0.25%
Mirato TOTS 0.5%
JM TOTS Moments 1%
Guto TOTS Moments 1%
Anselmo Gold IF 5%
Emanuel Gold IF 7%
i have this list
this dosent rlly make any sense?
imo anyway
a system that chooses one of these names from the list at random according to its chance
like a CS GO box
you have to use some random function if you want randomness, idk what you mean by "without randint"
can you share your code that you think is too complex?
maybe someone here can help you simplify it.
Use numpy.sample or I think random.sample has one for weights too
random.sample expects ints, so I've converted all your percentages to the appropriate weight.
from random import sample
values = (
"Gui",
"Schmidt",
"Peido",
"Thigas",
"Mirato",
"JM",
"Guto",
"Anselmo",
"Emanuel",
"Gui TOTS",
"Schmidt TOTS",
"Peido TOTS",
"Thigas TOTS",
"Mirato TOTS",
"JM TOTS Moments",
"Guto TOTS Moments",
"Anselmo Gold IF",
"Emanuel Gold IF",
)
weights = (
50,
50,
100,
100,
300,
750,
1000,
3000,
3130,
10,
10,
25,
25,
50,
100,
100,
500,
700,
)
print(sample(values, 1, counts=weights))
The 1 is just telling it to grab a single item at random using the weights.
Cool zech I didn't know about that
I prefer numpy.sample since you can just provide the probabilities, but of course it's not in the stdlib.
How does that handle a case where the probabilities sum to >100% or <100%?
Using weights you don't have those issues
thank you
that was it
just a question, how does the weights works?
If you have two items A and B, where item A has a weight of 1 and item B has a weight of 3, you'll get item A 1 out of 4 times and item B 3 out of 4 times. So the probability of any one item being selected is the item's weight over the sum of all weights.