Search code examples
phpmathlogarithmrandom

How can I add random “fuzz” to this formula, so that the randomness diminishes as values increase?


I'm developing an open-ended strategy game. I am using the following formula to calculate damage:

$rand = rand($a, $b) + $c;
$damage = $rand * sqrt(($d / 20) * $c));

$a, $b, $c, and $d are all values that can be modified by the user over the course of play, either by buying a better item ($a and $b), investing in the item ($c), or investing in their character $d.

What I want to do now is add a bit of randomness to the outcome of the equation. However, because the game is open ended:

  1. a static value would become unnoticeable/negligible over time.
  2. a percentage based value would allow for too much noise over time.

So, I want to add a random value that is small at first, grows with increased input, but has diminishing returns. I'm sure I need some kind of logarithmic formula, but I'm not sure how to go about it!


Solution

  • If you calculate your original damage as D and R is random in [-1,1], you have lots of options. You didn't want static:

    D = D + 1 * constant * R
    

    or percentage

    D = D + D * constant * R.
    

    Something in between might be

    D = D + sqrt(D) * constant * R.
    

    Any function of D between constant and linear will give you a different balance.