# Drawing items at random based on "weight"?

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
• I am trying to create a random system that is also based on "weight" of the items that are supposed to be drawn at random but beyond how it should work I am not sure how to actually implement it or do it in a good manner. This is how it should work:

Say you have 10 items and every time you "roll" a random check it should pick say 4 out of these 10 items (and no duplicates).

But instead of just drawing 4 items out of the 10 at complete random I also want the 10 items to each have a "weight" in how likely (or unlikely) they will be drawn.

So say we have this "weight" value and it is a value between 0 and 10 where the higher the value is the higher the probability that this item will be drawn on the next random check so an item with a weight value of 7 has a higher chance to be drawn than one with a weight of 3 for example (it could also be the reverse if that would make more sense for some reason in calculations). Probably the value could even be negative so that an item is even less likely to be drawn than the "base value" of 0 for rare items etc. Or another way to do this if negative values would result in problems would perhaps be that the base value is say 5 and then more rare items have a lower value and more common a higher value. Not completely sure what is the best approach for it.

I wonder if anyone have any suggestions on how to go about making such a system or examples of such a system?

I started to make an example that I have attached but I only got so far before I started to have problems with how to actually implement the different weight on the items to have some be more or less likely to be drawn.

## Files

• One way would be to pick your random from a string. Just add more of one letter for a higher chance.

Eg.

If you have 3 items A B C and you want,

A high prob

B medium prob

C low prob

"AAAAAABBBBCC"

pick a random letter from string

• If your game will be Windows-only, try the Random Multipool object. You add items to the pool as many times as you want (to increase their weighting). Then when an item is picked, you use the remove all occurrences of number action to prevent any future duplicates.

If you want to make it yourself, then you'd probably do something along the lines of stiggit's suggestion (though you may want to use a list object instead of a string if your choices are going to be more complex than single characters). Then when you pick an item, you'll need to remove all occurrences of that item from your pool (eg. using a combination of a fastloop and Fusion's native Find() function.).

Alternatively, you could potentially make use of the X chances out of Y function, though it may be tricky getting accurate weighting happening if you have to combine many such events for different objects:

If at all possible, I'd go with the Random Multipool option as it's likely to be the easiest.

Haven't tested it thoroughly, so can't guarantee it doesn't have bugs - you can let me know if you find any...
It works very much along the lines of what Stiggits suggested, but with a slightly different string structure:
[5:sword][50:potion][100:herb][75:empty][1:magic sword]

So basically, it's like you have a big bag containing 5 swords, 50 potions, 100 herbs, etc, and you reach in and pick one item totally at random. There are 20 times as many herbs in the bag as there are swords, so you're 20 times as likely to pick one.
When you pick an item, you can make it so that it has no effect on the probability of that item being picked again (the item goes back in the bag), the probability is reduced (the item does NOT go back in the bag), or that item can't be picked again (all items of that type are removed from the bag).

It's all done as a self-contained widget, just using 1 active object, so it'll work in any runtime.

Haven't tested it thoroughly, so can't guarantee it doesn't have bugs - you can let me know if you find any...
It works very much along the lines of what Stiggits suggested, but with a slightly different string structure:
[5:sword][50:potion][100:herb][75:empty][1:magic sword]

So basically, it's like you have a big bag containing 5 swords, 50 potions, 100 herbs, etc, and you reach in and pick one item totally at random. There are 20 times as many herbs in the bag as there are swords, so you're 20 times as likely to pick one.
When you pick an item, you can make it so that it has no effect on the probability of that item being picked again (the item goes back in the bag), the probability is reduced (the item does NOT go back in the bag), or that item can't be picked again (all items of that type are removed from the bag).

It's all done as a self-contained widget, just using 1 active object, so it'll work in any runtime.

Bit of a late reply but thanks for all the input!

To be sure I get the hang of this myself I decided to do my own example based on a list example that is very simple. I have managed to fill in the list with two "abilities" that should be drawn and one has a weight of 5 and the other of 3. Then you draw a random number from the list. The problem though is that when I draw the number it only removes one instance of the number. So if the list is filled in with 00000,111 for the two abilities and I press the random button and it selects 1 it only removes 1 "1" from the list instead of all the "1":s.

How do I make it so it cleans out all the "1":s in this case?

Volnaiskra mentioned using the "Find() function" but I have no idea on how to actually use this function in practice to achieve this? I have attached an example of what I have so far

## Files

• You just have to run a fastloop to search for all the other copies of the same value, and delete those as well.

Or using a slightly different method, that ought to be a bit more performant for very large lists: Please login to see this attachment.

• You just have to run a fastloop to search for all the other copies of the same value, and delete those as well.

Or using a slightly different method, that ought to be a bit more performant for very large lists: Please login to see this attachment.

Perfect thank you so much!

• looks like you may have got closer to what you want. but i 'll share what i did with something a long time ago which is a very simplified way of "weighting chances of selection".

i had a [result] value empty at start;

i would check in order the most common outcomes first; and sometimes multiple times to increase their chance of being picked... ( this is all before the days of X chances in Y features)

eg;

if result value = 0

random(10)<3 result value = item1

random(10)<3 result value = item1

random(10)<2 result value = item2

etc, etc, etc...

if result value still = 0 -> result value = supercommon item

it was basic, but it worked and you had to predetermine what was going to be more important or common.

## Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!