在Go语言中实现非均匀分布的随机数。

huangapple go评论74阅读模式
英文:

Non-uniform distribution random in golang

问题

引用自英雄联盟

暴击的概率会根据攻击的连续暴击次数动态变化。如果一次攻击没有暴击,那么下次攻击的概率会逐渐增加;反之,如果多次攻击都暴击成功,下次攻击的概率会逐渐减小。

如果我理解正确的话,事件出现的概率会逐渐受到之前次数的影响,即非均匀分布的随机化,对吗?在golangmath/rand中是否有这样的随机化算法?如果没有,我们该如何实现它?

英文:

Quoted from league of legends:

> The probability of a critical strike changes dynamically based on how many times the attack critically strikes. If an attack does not critically strike over multiple attempts, the probability will progressively increase for future attempts—this can occur vice versa, where multiple successful critical strikes will cause the probability to progressively decrease for future attempts.

If I understand correctly, the chance for an event to appear is progressively affected by previous times, i.e. non-uniform distribution randomization, right? Is there such a randomization algorithm in golang's math/rand? If not, how can we implement it?

答案1

得分: 2

这个实现起来似乎很简单,只需要使用rand.Float32()rand.Float64()。没有看到你的代码,很难给出具体的代码。

你可以简单地重复采样均匀分布的浮点数,并将其与不断变化的成功概率进行比较。在未命中时,成功概率增加;在命中时,成功概率减少。

例如:

func did_crit_hit(prob_success *float64) bool {
    p := *prob_success
    hit := rand.Float64() < p
    if hit {
        p = math.Max(0, p - 0.1)
    } else {
        p = math.Min(1, p + 0.1)
    }
    *prob_success = p
    return hit
}

你可能希望进行更复杂的操作,而不仅仅是固定增量的改变,但希望这能给你一个思路。

英文:

This seems trivial to implement given rand.Float32() or rand.Float64(). Without seeing your code it's difficult to give much code.

You can just repeatedly sample uniform floats and compare to a varying probability of success. This probability goes up on misses and down on hits.

For example:

func did_crit_hit(prob_success *float64) bool {
	p := *prob_success
	hit := rand.Float64() &lt; p
	if hit {
		p = math.Max(0, p - 0.1)
	} else {
		p = math.Min(1, p + 0.1)
	}
	*prob_success = p
	return hit
}

you might want to do something more complicated than just changing by a fixed increment, but hopefully that gives you an idea.

huangapple
  • 本文由 发表于 2021年7月15日 23:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/68397094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定