Getting a random number with a distribution function

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 2K views
oneamp
Messages
222
Reaction score
0
I have a mean mu, and an exponential distribution function. How do I use a random number, generated with a PRNG, to get a random number from the distribution? I know this is a really basic question. Please help :)

Thanks
 
Physics news on Phys.org
-(mu)*ln(1-rand()) is that about right?
 
You need to know what the cumulative distribution is. If the CDF is F(y), then if you pick a number rand() uniformly at random from [0,1] you need to find the number y such F(y) = rand(). What you have written in your second post is the correct formula for inverting the CDF of an exponential distribution.
 
oneamp said:
-(mu)*ln(1-rand()) is that about right?

What is definition of the function rand() in the programming language that you are using?
 
rand() is just hypothetical; it generates a 'random' number between 0 and 1. Since what I've written in the second post is the correct formula for inverting the CDF (Thank you office shredder), is this a correct way that I can generate random numbers according to this distribution? I am confused that the web shows lambda where I show mu, for example...

Thank you
 
oneamp said:
rand() is just hypothetical; it generates a 'random' number between 0 and 1.

If that's your definition of rand(), then you're OK. As defined in some programming languages rand() returns an integer value.

Since what I've written in the second post is the correct formula for inverting the CDF (Thank you office shredder), is this a correct way that I can generate random numbers according to this distribution?

Yes, it's the correct theoretical way. In an actual program, you must worry about whether rand() (the function implemented on the computer, not the theoretical uniform distribution) has a nonzero probability of returning exactly 1, which creates the problem ln(0).