Seeking a function with value 0 at origin and infinity, smoothly rising to peak at x=n, and smoothly falling after that, area (0 to infinity) = 1

91 Views Asked by At

Obviously there might be many functions satisfying this, but I'm wondering what simpler ones might satisfy these desiderata, and seeking answers for both the discrete and continuous case. My motivation is actually a new idea for how to calculate damage done by weapons in RPGs. These are often, say, 1-6, where a single die roll tells you how much damage is caused to a target. But I'm intrigued by the idea of a weapon where the most likely damage is n, but has a increasingly-small but finite chance of causing more damage (m, where m is any number > n); of course there will also be some chance of causing damage between 0 and n.

Of course, a continuous function can be calculated by a computer, and then rounded down; this wouldn't be conducive for tabletop games, but perhaps can give us an idea of how to approximate such a function via dice. One rough approximation to this I thought of years ago is simply rolling regular dice: 1-5 gives that much damage, 6 = 5 + roll the die again and add that much (if you get 6 again, the function is recursive so you have 10 and roll again, etc.) This isn't smooth since the probability of getting 1-5 is equal, but it satisfies the other desiderata. I believe the average (mean) result this gives is 3.5 just like an ordinary die roll, but can rise to arbitrarily large results; you could arbitrary increase this for character strength, weapon size, etc. by either adding or multiplying some factor to this. Any thoughts for smoothing this out further?

4

There are 4 best solutions below

3
On

Answering the question in the title: What about $f(x)=\dfrac x{n^2}e^{-x/n}$?

1
On

If $f(x)=x^ne^{-x}$ then $f(0)=0$, $f'(x) =nx^{n-1}e^{-x}-x^ne^{-x} =x^{n-1}e^{-x}(n-x) $ so $f'(n)=0$ and $\int_0^{\infty} f(x)dx =\int_0^{\infty} x^ne^{-x}dx =n!$ so $\dfrac1{n!}x^ne^{-x}$ works.

(The traditional Gamma integral)

3
On

Rolling the die in the fashion you suggested lacks smoothness because it's only a slight departure from a uniform distribution. One way to escape the uniformity is adding another die. Rolling two dice and summing their faces, for example, results in a triangular distribution.

One route you could take: I have some vague notion that tetrahedral dice are sometimes used in these contexts. Label the faces of one $1, 2, 3, 4$. Label the faces of the other $1, 2, 3, \text{"roll again"}$. Roll both dice, and if the second die comes up $\text{"roll again"}$, add $3$ to the first die's value and roll the second again. Repeat rolling the second die as appropriate. Here is the simulated data from Python:

data = []
for k in range(10000) :
roll = random.randint(1,4)
keep_going = True
while keep_going :
    d2 = random.randint(1,4)
    if d2 == 4 :
        roll += 3
    else :
        roll += d2
        keep_going = False
data.append(roll)

Here's what the distribution looks like:

Histogram of simulated data

The histogram stops around 25, but it's clear that any positive integer is possible. Some issues: The peak may not obtain at the desired $n$; it's smoother than your original method, but perhaps it could be smoother; and the variance may not be as large as you'd like. But perhaps this provides a starting place.


As for a continuous function: How about

$$ f(x) = \frac{4\sqrt{3}n^2}{\pi}\frac{x}{x^4 + 3n^4}? $$

I'm not sure it's pretty, but it's rational (even if it does have an irrational constant out front). Moreover, $ \int_0^\infty f(x) \; dx = 1 $ and its max is at $ x = n $. The final advantage I'll note later.

I knew that something of the form $f(x) = k \cdot \frac{x}{x^4 + a}$ would have $f(0) = 0$; would have an integral that converged; would have infinite support -- i.e., $f(x) > 0 $ if $x > 0$; and would have a peak for some $x > 0$.

If we want the max to be at $x = n$, we must have $a = 3n^4$. That's not terrible. If we then want the integral to equal $1$, well, $k = \frac{4\sqrt{3}n^2}{\pi}$, which is less ideal :/.

But what do we do with this function? In order to generate numbers that are distributed according to some density function $f(x)$, the first thing we need is a cumulative distribution function -- that is, the function that tells us the probability of getting a number less than some given $x$-value. In the above case, this is simply

\begin{align} F(x) &= \int_0^x f(t) \; dt \\ &= \frac{4\sqrt{3}n^2}{\pi} \int_0^x \frac{t}{t^4 + 3n^4}\; dt \\ &= \frac{2}{\pi} \arctan \left( \frac{x^2}{\sqrt{3}n^2} \right). \end{align}

The second thing we need is a number drawn uniformly from $[0,1]$. On the one hand, we've traded the problem of generating a specific type of random number for the problem of generating some other type of random number. However, uniform distributions are easy. For example, a roll of a normal die gives us a number drawn uniformly from $\{ 1, 2, \ldots 6 \} $. Let's say we want a number drawn uniformly from $\{ 0, 0.001, 0.002, \ldots 0.999 \}$. We could roll a ten-sided die three times, each roll giving us a decimal place. Each sequence of three rolls would combine to give us a number -- e.g., the sequence $3, 2, 6$ would yield the number $0.326$ -- and each number we end up with would be just as likely as any other number.

Now, if you can easily write $F^{-1}(\cdot)$, the inverse of the cdf from your desired distribution, then you can generate numbers from that distribution.

I'll write $X$ for the number we want to generate and $Y$ for the outcome of rolling the ten-sided die three times. If we set $X = F^{-1}(Y)$, we'll have the kind of number we want.

Why is this? First, write $G(y) = P(Y \le y)$ for the cdf of $Y$. Hopefully it is somewhat intuitive to see that $G(y) = y$: The probability that our number is in the bottom half is $0.5$; the probability it's in the bottom $22$ percent is $0.22$; etc. It's a flat distribution.

Next, if $X = F^{-1}(Y)$, we have

$$P(X \le x) = P[F^{-1}(Y) \le x] = P[Y \le F(x)] = G[F(x)] = F(x).$$

If $X = F^{-1}(Y)$, we also have $Y = F(X)$, which is a relation we can explicitly write out:

$$Y = \frac{2}{\pi} \arctan \left( \frac{X^2}{\sqrt{3}n^2} \right) $$

Solving for $X$ in the usual way gives us:

$$ X = \sqrt[4]{3} n \sqrt{\tan \left( \frac{ \pi Y }{2} \right) }, $$

where the right-hand side is precisely $F^{-1}(Y)$.

So what does that mean? Generate a $y$-value by rolling a ten-sided die however-many times. Plug that number into $ F^{-1}(y) = \sqrt[4]{3} n \sqrt{\tan \left( \frac{ \pi y }{2} \right) } $. And after these two steps, you'll have an $x$-value from your desired distribution.

For example, if $n = 6$, $F^{-1}(0.326) \approx 5.92 $, which you could round to $6$.

(If the above function is too messy, you could probably get away with $F^{-1}(y) = 1.32n \sqrt{\tan(1.57y)} $. I might add: Put your calculator in radian mode :).)

While the above function $f(x)$ is a little messy, its main advantage is that the inverse of its antiderivative can be written easily. In fact, unless you're doing some kind of numerical integration on a computer, I'm not sure you'd need $f(x)$ at all -- or any density function.

0
On

One rough approximation to this I thought of years ago is simply rolling regular dice: 1-5 gives that much damage, 6 = 5 + roll the die again and add that much (if you get 6 again, the function is recursive so you have 10 and roll again, etc.) This isn't smooth since the probability of getting 1-5 is equal, but it satisfies the other desiderata.

HackMaster calls your system "penetrating" dice, but I've rarely seen it outside that system. Most games just add the entire 6 to the recursive rolls, which creates gaps in the distribution at 6, 12, 18... but is more intuitive as a physical process on the table. This is usually called "exploding" dice. The most popular recent RPG to use exploding dice as a core mechanic is probably Savage Worlds.

You can smooth out the ramp by adding a few such dice together, which also helps hide the gaps. Probably the most famous RPG to use this scheme is Earthdawn. Here's 3d6 with explosions on AnyDice.com.

output 3d[explode d6]

graph