# Generating random numbers that are log-normally distributed

Even though I don't quite understand why and how this works, I appreciate how simple it is to generate a set of numbers which are Poisson distributed:

public static int getPoisson(double lambda) {
double l = Math.exp(-lambda);
double p = 1.0;
int k = 0;

do {
k++;
p *= Math.random();
} while (p > l);

return k - 1;
}


This "generating" function is basically from this book were I also find generating functions for the binomial and the geometric distribution.

But I did not find a simple generating function for the log-normal distribution. Either the author calls this distribution the "logarithmic series distribution" (then the generating function would not be simple), or it is missing. In the latter case I am looking for some pseudo-code.

• You can probably find details about how to create a random number generator for a normal distribution; then you just have to take the exponent of it. Aug 5, 2020 at 14:19
• @jwimberley: do you want to post your comment(s) as an answer? Better to have a short answer than no answer at all. Anyone who has a better answer can post it. Aug 5, 2020 at 14:21
• The logarithmic series distribution is a completely different beast. Aug 5, 2020 at 15:32

## 1 Answer

Since the log-normal distribution is defined as the distribution of exp(X) where X is a random variable following a normal distribution, you need only find details about how to sample a random number from a normal distribution, and take its exponent.

• Sorry, but don't you mean "take its logarithm"? Aug 5, 2020 at 14:33
• No, I do not. If If X is log-normally distributed, then log(X) is normally distributed, which is what you are probably thinking of. But I'm describing generating a normally distributed Y=log(X) directly. You couldn't take the logarithm of a normally distributed value, anyways, since the normal distribution has support on the negative real numbers, outside the domain of the logarithm. Aug 5, 2020 at 14:38
• The term lognormal is in a sense backward as a lognormal distribution is, as said, an exponentiated normal. The lognormal is named for what you can do with it -- take logarithms, and you get a normal -- rather than for what you need to do to get it. Although it would be appealing to start all over again with statistical terminology -- the term normal is itself a source of much confusion and misunderstanding -- in practice we have to muddle through and hope that with slow campaigns some usages will fade away and better terms will take their place. Aug 5, 2020 at 15:14
• Devroye discusses generating from the normal distribution in chapter 9. In the section on normal distributions, an exercise about lognormal distributions (p. 392) mentions this approach, and also hints at a faster/more direct approach to generating lognormal variates. Aug 5, 2020 at 16:32