duong_dajgja
duong_dajgja

Reputation: 4276

Non-uniform distribution random in golang

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?

Upvotes: 0

Views: 849

Answers (1)

Sam Mason
Sam Mason

Reputation: 16184

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() < 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.

Upvotes: 2

Related Questions