使用重量修饰符创建加权随机系统
我试图使用C#为我的游戏创建一个加权随机系统。
玩家从N事件开始,但是随着游戏的进行,事件的数量增加。最初,所有事件都是同样可能的,概率为1/n,但是玩家可以使用修饰符使某些事件更有可能,例如某些事件的发生概率两倍,或者在某些情况下,无论如何,它们都可以强迫事件发生。
我发现,进行加权随机的广泛方法是总和所有权重,生成随机数达到权重的总和,然后迭代选择,然后累积权重,直到累加器&gt>生成的值。
现在,我可能对此有所思考,但是对我来说,这种方法似乎存在一些问题。
例如: 玩家有7个选择,每个都有1/7的重量,总权重为1。现在,如果1个事件获得双重修改器,则其新概率为2/7,重量为8/7。这使我认为我应该从其他事件的重量中减去1/42,以使总和保持为1。否则,我认为这实际上不是双重概率。
另一个例子: 玩家强迫事件发生100%。这意味着1事件的重量为1,而另一个事件的权重为0。现在,它取决于随机函数的实现发生0的事件可能发生。
如果您有通知,我将权重保持在0到1之间。这使我可以互换使用权重和概率,因为重量为1/7,也意味着概率为1/7。另外,我不必计算权重的总和,因为它总是1
。
附加说明: 你们中的一些人可能建议根据从最大到最小的权重订购事件列表。由于RNG系统是游戏玩法的核心,并且经常发生,因此在性能方面可能不会对事件进行分类,因为每次都会拔出新的随机播放时,游戏中可能会有不同的修饰符。
同样,如果在7个选择示例中,当我将1个事件的机会加倍,而不是2/7时,我会使用2/8,而其他所有事件则是1/8,那么实际上它的概率并不是2/7> 2/8。
Im trying to create a weighted random system for my game in unity using C#.
Player starts with N events, but as the game goes on the amount of events increases. At first all the events are equally likely, probability 1/N, but the player can use modifier to make some event more likely, like some events have double the probability to occur or in some cases they can force an event to happen no matter what.
I have found that widespread approach to do weighted randoms is to sum all the weights, generate random number up to the sum of weights and then iterate over choises and accumulate the weights until the accumulator > generated value.
Now i might be overthinking this, but to me it seems like there are some issues with this approach.
For example:
Player has 7 choises, each has a weight of 1/7 and sum weights is 1. Now if 1 event gets a double modifier its new probability is 2/7 making the sum of weights 8/7. This leads me to think that i should subtract 1/42 from the weights of the other events to keep the sum as 1. Otherwise its not actually double probability, i think.
Another example:
Player forces an event to occur 100%. This means 1 event has weight of 1 and all the other has weight of 0. Now it depends a bit of the implementation of the Random function but in theory nothing stops the random from spewing out 0 and then depending on the order of the events any event with 0 can happen.
If you have notices i have kept the weights between 0 and 1. This allows me to use weights and probabilities interchangeably since weight of 1/7 also means probability of 1/7. Also this way i dont have to calculate the sum of the weights, since its always 1.
Am i overthinking the problem and its actually not that complex?
Additional note:
Some of you might suggest ordering the list of events based on the weights from largest to smallest. Since the RNG system is core of the gameplay and happens very often it might not be wize in performance perspective to sort the events, given that there might be different modifiers in play everytime a new random is pulled.
Also if in the 7 choise example, when i double chance of 1 event and instead of 2/7 i would use 2/8 and everything else is 1/8 then its actually not double the probability as 2/7 > 2/8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下: https://github.com/cdanek/kaimira-weighted-list-list-list-list-list-list-list-list
免责声明:我写了它,但这正是您想要的。
它不需要多次进入列表才能进行操作。该解决方案来自O(1)中的加权列表的结果。它(希望)在琐碎的实施上也很简单。
Try this: https://github.com/cdanek/kaimira-weighted-list
Disclaimer: I wrote it, but it's exactly what you're looking for.
It doesn't need to hit the list multiple times for a get operation. This solution gets a result from a weighted list in O(1). It's also (hopefully) trivially simple to implement.