I need to write some code that calculates a future random date/time (i.e. essentially a period of time), and I'm looking for an appropriate probability distribution and function I can use to transform a pseudorandom floating-point number between 0 and 1 into a period of time I can add to the current date/time. My requirements are:
- Probability at current time is zero, goes up to a maximum, then ideally falls asymptotically towards infinity, but I guess reaching zero would be okay too.
- Shaped similar to a normal distribution, although that obviously does not reach zero probability at any point so would not seem to work.
- Can be parameterised by ideally the mode or alternatively the mean of the distribution.
- Ideally it would be a continuous distribution, and the date/time could therefore be any time, obviously subject to the digital precision of my date/time representation. I would consider discrete functions, but I'd ideally be able to make it quite fine-grained.
- Needs to be reasonably performant. This is written in an interpreted language (Ruby, if anyone's interested) so I'm not looking to write any loops in my code if possible. But if the function is common enough I can probably find performant native code for it. Any mathematical advice would likely also help me understand this well enough to find any existing solutions I can leverage.
Through some initial research I've discovered the gamma distribution, which seems to satisfy the shape and continuity criteria, but it's not entirely clear to me how to calculate it. Wikipedia mentions this is used for "random time until death" calculations, so it sounds appropriate.
In order to convert from a random number between 0 and 1 into my period of time, I believe I'd need to calculate the inverse cumulative distribution function of the gamma distribution, and I'm not sure how, except of course with bisection, which I'm thinking won't be ideally performant.
Also while I have a library that can calculate the gamma function, I don't see anything about the lower incomplete gamma function (which I see in the cumulative distribution function definition) and am not sure if I can implement this easily.
It looks like I might be able to parameterise the gamma distribution based on a specific mean somehow, but I'm not quite sure how.
So are there any other functions that I should consider? Or if the gamma function is the best, how might I overcome these problems?
My knowledge about statistics and calculus is likely highly rusty, and analysis is non-existent. This isn't the area of mathematics that my interest normally lies. So apologies if this is poorly worded or an obvious answer. I've definitely got to an "eyes glazed over" point at the moment.
To generate a gamma distributed random variable with integer shape parameter of $k$ and with mean $\mu$, you can take $k$ random variables $X_1,X_2,\ldots, X_k$ independently and identically uniformly distributed on $(0,1)$ and consider $$\displaystyle - \frac{\mu}{k} \sum_{i=1}^k \log_e(X_i)$$
If you also had a target standard deviation $\sigma$ then you could want to set $k= \frac{\mu^2}{\sigma^2}$, though this needs to be an integer for the simple sum above so you would then need to round it.