Need for predictable random generator

前端 未结 30 1059
情话喂你
情话喂你 2020-11-27 09:04

I\'m a web-game developer and I got a problem with random numbers. Let\'s say that a player has 20% chance to get a critical hit with his sword. That means, 1 out of 5 hits

30条回答
  •  轮回少年
    2020-11-27 09:31

    Well, if you are into math a little, you can probably try Exponential distribution

    For example, if lambda = 0.5, expected value is 2 (go read that article!), means you will most probably hit/crit/whatever every 2nd turn (like 50%, huh?). But with such probability distribution, you will definetevely miss (or do opposite to whatever) at 0th turn (the one, in which event had already occured and turn_counter had been reseted), have like 40% chance to hit next turn, about 65% chance to do it 2nd (next after next) turn, about 80% to hit 3rd and so on.

    The whole purpose of that distribution is if one has 50% hit chance and he misses 3 times in a row, he wil shurely (well, over 80% chance, and it increases every next turn) hit. It leads to more "fair" results, keeping overal 50% chance unchanged.

    Taking your 20% chance of crit, you have

    • 17% to crit 1st turn
    • 32% to crit 2nd turn, if no crit occures in all previous ones.
    • 45% to crit 3rd turn, if no crit occures in all previous ones.
    • 54% to crit 4th turn, if no crit occures in all previous ones.
    • ...
    • 80% to crit 8th turn, if no crit occures in all previous ones.

    Its still about 0.2% (vs those 5%) chance of 3 crits + 2 non-crits in 5 consequent turns. And there is 14% chance of 4 consequent non-crits, 5% of 5, 1.5% for 6, 0.3% for 7, 0.07% for 8 consequent non-crits. I bet its "more fair" than 41%, 32%, 26%,21% and 16%.

    Hope you still don't bored to death.

提交回复
热议问题