#๐Ÿ”’ Random numbers on weighted mods

52 messages ยท Page 1 of 1 (latest)

fading verge
#

Howdy, Im trying to recreate a crafting system from a game I play, it consists of modifiers that can be on an item. Each modifier has the mod name, tags that associate it with certain type (life, cold, speed etc), and then it has a weight. Im trying to figure out a good way to do hit a random modifier that aligns with the weights. I was thinking I could just have a dict with the mods as a key and their tags,weight as values, and have it sorted by the weight, and roll a random number 0-maxweightvalue, go through the dict, adding the weights it passes through as it goes, and if it hits that number thats the mod itll be. Im not sure if this is a good way, or even a working way. Any ideas?

flat flowerBOT
#

@fading verge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

harsh storm
#

!d random.choices

flat flowerBOT
#

random.choices(population, weights=None, *, cum_weights=None, k=1)```
Return a *k* sized list of elements chosen from the *population* with replacement. If the *population* is empty, raises [`IndexError`](https://docs.python.org/3/library/exceptions.html#IndexError).

If a *weights* sequence is specified, selections are made according to the relative weights. Alternatively, if a *cum\_weights* sequence is given, the selections are made according to the cumulative weights (perhaps computed using [`itertools.accumulate()`](https://docs.python.org/3/library/itertools.html#itertools.accumulate)). For example, the relative weights `[10, 5, 30, 5]` are equivalent to the cumulative weights `[10, 15, 45, 50]`. Internally, the relative weights are converted to cumulative weights before making selections, so supplying the cumulative weights saves work.
harsh storm
#

There exists here a weights parameter.

#

I'm a little fuzzy on how cum_weights works, but I've never had cause to need it over weights.

#

!e py import random choices = 'Apples', 'Pears', 'Bananas' weights = 1, 2, 3 result = random.choices(choices, k=10) print(result)

flat flowerBOT
harsh storm
#

Oh, that's special.

#

I weighted bananas the most.

#

But do we see any bananas?

#

The gods of statistical improbabilities smile on us, today.

#

Oh.

#

No, hang on.

#

I didn't use weights.

fading verge
#

Does this work with a dictionary tho? Cuz theres a ton of other stuff that I need to associate with these mods

harsh storm
#

Let's try that again.

#

!e py import random choices = 'Apples', 'Pears', 'Bananas' weights = 1, 2, 3 result = random.choices(choices, weights, k=10) print(result)

flat flowerBOT
harsh storm
#

That's better. Still. To not have bananas show up at all in the first one was a little flukey.

#

As to having a population, the object given has to be indexable.

#

Dictionaries are possible but ill-suited.

fading verge
#

Are they? How else should I store all this info though

#

Like for example

#

omg its so much to write out even for the example

#

would some sort of database type thing be better like sql?

harsh storm
#

I'm not databasy.

fading verge
#

This is one mods worth of info I need stored for one item type (body armor) in this case

#

Its got a tag, a range, a tier, an ilvl, a weight, a prefix %, and a weight %

harsh storm
#

That could live in a list of tuples of mixed elements, easily enough.

#

In file, as csv.

fading verge
#

oh no..

#

im getting ptsd from school

#

lmao

#

i hated csv so much

harsh storm
#

It is a touch fiddly.

fading verge
#

Not impossible tho yeah

#

itll be a pain writing all this over tho

#

oh well

#

it is what it is

harsh storm
#

If you have something like Excel or LibreOffice, creating a csv table from copying and pasting wouldn't be difficult.

#

Or even just a plaintext editor.

fading verge
#

well just in general theres a lotta mods and such x3

slow python
#

dont forget you can read excel files from python as well

#

not sure what you made this in but you can make it work

fading verge
#

I havent started yet, Im just trying to brainstorm rn

#

Its a big big project for me

#

https://www.craftofexile.com

Im trying to recreate this pretty much

#

And then once thats done I have a further plan for it

flat flowerBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.