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?
#๐ Random numbers on weighted mods
52 messages ยท Page 1 of 1 (latest)
@fading verge
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.
Closes after a period of inactivity, or when you send !close.
!d random.choices
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.
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['Apples', 'Pears', 'Apples', 'Pears', 'Pears', 'Apples', 'Pears', 'Apples', 'Apples', 'Apples']
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.
Does this work with a dictionary tho? Cuz theres a ton of other stuff that I need to associate with these mods
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['Bananas', 'Pears', 'Bananas', 'Apples', 'Pears', 'Apples', 'Bananas', 'Pears', 'Pears', 'Pears']
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.
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?
I'm not databasy.
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 %
That could live in a list of tuples of mixed elements, easily enough.
In file, as csv.
It is a touch fiddly.
Not impossible tho yeah
itll be a pain writing all this over tho
oh well
it is what it is
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.
well just in general theres a lotta mods and such x3
dont forget you can read excel files from python as well
not sure what you made this in but you can make it work
I havent started yet, Im just trying to brainstorm rn
Its a big big project for me
Im trying to recreate this pretty much
Craft of Exile is a crafting simulator for Path of Exile designed to compute the probabilities of obtaining specific results through different methods.
And then once thats done I have a further plan for it
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.