#percentage in random module

26 messages · Page 1 of 1 (latest)

weak charm
#

there's a way to make a percentage system without using randint? it's taking a lot of work

#

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

sweet orchid
#

imo anyway

weak charm
weak charm
#

like a CS GO box

teal saddle
#

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.

silver ferry
#

Use numpy.sample or I think random.sample has one for weights too

proud vapor
#

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.

teal saddle
#

Cool zech I didn't know about that

silver ferry
#

I prefer numpy.sample since you can just provide the probabilities, but of course it's not in the stdlib.

proud vapor
#

How does that handle a case where the probabilities sum to >100% or <100%?

#

Using weights you don't have those issues

silver ferry
#

It doesn't, it would error.

#

They have to sum to 100.

weak charm
proud vapor
#

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.