Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random number between 0 and 1 with (negative)exponential distribution

Heyho,

I'm trying to generate a random number between 0.0 and 1.0 with exponential/negative exponential distribution. There is an article, which tells that you have to get the "quantile function". But the results are still greater than 1.0. So I would need to scale my equation somehow.

My goal is to generate a random number in a range, where for example higher/lower values have a higher probability. (The distribution should be scalable)

Related questions(don't truncate the result to [0,1]):

  • Pseudorandom Number Generator - Exponential Distribution
  • Generate random number between 0 and 1 with guassian distributes
like image 717
maxammann Avatar asked Feb 15 '23 11:02

maxammann


2 Answers

The negative exponential distribution has support on the range [0, ∞), so I'm interpreting your question as a request for a truncated negative exponential. The Cumulative Distribution Function for such a beast with lower limit l >= 0, upper limit h > l, and rate λ is

F(x) = (exp(-λl) - exp(-λx)) / (exp(-λl) - exp(-λh))

We can find the inversion by setting this equal to U, a uniform(0,1) random number, and solving for x:

X = -ln(exp(-λl) - (exp(-λl) - exp(-λh)) * U) / λ

Since you specified lower and upper limits of 0 and 1, respectively, this reduces to

X = -ln(1 - (1 - exp(-λ)) * U) / λ

Replace U with a call to your favorite U(0,1) generator and that's your algorithm for generating X's with the desired distribution.

Here's a histogram of 10,000 values generated with λ = 5. Smaller values of λ give a flatter distribution, larger values show a more rapid exponential drop off.

10,000 truncated exponentials with lambda = 5

like image 157
pjs Avatar answered Feb 17 '23 01:02

pjs


Based on the comment that you want both lower and higher numbers to have a higher likelihood:

// let u,v be random real numbers from [1, 10]
x = log(u) // x is from 0.0 to 1.0, with higher probability getting higher values.
y = 1 - log(v) // y is from 0.0 to 1.0, with higher probability of getting lower values.
if abs(x - 0.5) > abs(y - 0.5):
    return x
else:
    return y

This is a bit hacky, but it makes it extremely unlikely to get a value exactly in the middle 0.5 and likely to get the edge values. It seems to fit your requirements though.

EDIT: It could use some fine tuning when x == y. You could use another random choice to determine which to select in that case:

if x == y:
    // let w be a random number either 1 or 2.
    if w == 1:
        return x
    else:
        return y

Additionally, if the application calls for this function to be a bit more or less polar, it can be adapted to do so:

// let u,v be random real numbers from [1, K] where K > 1 
// and let j be a real number given by log(K).  j is selected by the function's caller.
x = log(u) / j
y = (1 - log(v)) / j
// The remainder of the formula is identical.

By using a value like 2 for j, K = 100 and thus is more likely to be a polar value. If a value less than 10 is used for j, it will be less likely to be a polar value. In this way you can control the "slope" of the function.

like image 32
BlackVegetable Avatar answered Feb 17 '23 02:02

BlackVegetable