Random with probability

You could achieve this with the “random bool with weight” node

3 Likes

I have an array of several objects. I need a function that would pull out one of these things at random, BUT one float variable inside these objects needs to affect the probability of choosing this one object. So if variable in one object is set to 100, and others are 0, it needs to be result of the roll in 80% of rolls (for instance).

So I cast to objects in the array and extract the values of variables from them in a ForEachLoop sequence. Where do I go from here?

Could you elaborate please? Bool would enable me to compare two items at max, so I would need to compare every array item with every other array item?

Although I guess in my case it would be easier to just find the item with highest value and pit it in a branch against everything else which would be chosen at true random… But that doesn’t sound too elegant of a solution.

This could work. But I wonder if the order of Array elements will have an impact on that? Suppose you have seven elements in it and only one of them, the second to last one, will have a high variable. So before the loop gets to it, it will have to go through 5 loops where it will either chose or not chose any of those five elements. It seems that it will result in a system that the further the element is indexed in the array, the less are chances of it being chosen, regardless of it’s actual value.
Maybe it would help that in my case most likely there will be only one element with highest value in a given array, since player can’t be in two places simultaneously.

To give an entry ‘weight’ you could enter it multiple times. Then all you need to do is shuffle the array… :slight_smile:

Will something like this work for you? I directly put in floats, but you could to a structure and then cast to the probability part in your struct to get the associated item.

1 Like

Huh. This really sounds like a simplest of solutions! And the amount of times entry is re-added can be derived from the entry’s value!