Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: generate lognormal RV less than 20000 observations.

 From jhilbe@aol.com To statalist@hsphsun2.harvard.edu Subject st: RE: generate lognormal RV less than 20000 observations. Date Tue, 13 Apr 2010 14:22:26 -0400

I get the stata digest and do not always have time to look at the entries. I just noticed this discussion and since it relates to a little program I first wrote back in 1993 when I started to write a group of pseudo-random number generators for Stata.
```
```
I am confused about the requirement of 20000 observations. For all of the generators I just gave an arbitrary observation number for examples. There should be no limits on how many - or few -
```pseudo-random numbers are generated - whatever is allowed by your memory

```
Here is an example of generating 10 observations of a lognormal pseudo-random variate with a mean
```of 0 and a SD of .5. Am I missing something?

. rndlgn 10 0  .5
( Generating . )
Variable xlgn created.

. list

+-----------+
|      xlgn |
|-----------|
1. | 1.3562985 |
2. | .46046644 |
3. | .37838322 |
4. | .94114434 |
5. | 1.6200692 |
|-----------|
6. | 1.1143121 |
7. | .52069493 |
8. | 1.4824923 |
9. | 2.7165885 |
10. | .35047758 |
+-----------+

```
As a side comment, the more challenging generators that can be used to construct, for example, synthetic gamma or inverse Gaussian models, all use the rejection method. It's interesting to see the differences when comparing with Stata's new random number generators, which do not use this method for their construction. When creating synthetic models, however, both methods seem to produce the same models. There are still a few of the old suite of "random number generators" which do not have a corresponding generator in Stata.
```
Joseph Hilbe

Date: Sun, 11 Apr 2010 17:53:10 +0100
From: "Nick Cox" <n.j.cox@durham.ac.uk>
Subject: RE: st: RE: generate lognormal RV less than 20000 observations.

First, "Sorry, I just updated my rndlgn; the old one does require 20000
or more."

There is some misunderstanding by Sun here.

For a long time, the -rnd- package by Joseph Hilbe and W. Linde-Zwirble
was the most developed way to get random deviates in Stata. -rnd-
includes -rndlgn-.

Typing

. findit rndlgn

reveals the facts.

Search of official help files, FAQs, Examples, SJs, and STBs

STB-41  sg44.1  . . . . . . . . . . . . Correction to random number
generators
```
(help rnd if installed) . . . . . . . . J. Hilbe and W.Linde-Zwirble
```       1/98    p.23; STB Reprints Vol 7, p.166
faster version plus minor changes

STB-28  sg44  . . . . . . . . . . . . . . . . . . . . Random number
generators
(help rnd if installed) . . . . . . . .  J. Hilbe and W.
Linde-Zwirble
11/95   pp.20--21; STB Reprints Vol 5, pp.118--121
programs to implement random number generators that allows user
to generate synthetic data from a variety of distributions

Web resources from Stata and other users

(contacting http://www.stata.com)

sg44_1 from http://www.stata.com/stb/stb41
STB-41 sg44_1.  Correction to random number generators. / STB insert
by /
```
Joseph Hilbe, Arizona State University; / Walter Linde-Zwirble, Health Outcomes Technologies. / Support: atjmh@asuvm.inre.asu.edu / After installation, see help rnd.
```
sg44 from http://www.stata.com/stb/stb28
```
STB-28 sg44. Random number generators. / STB insert by / Joseph Hilbe, /
```   Walter Linde-Zwirble. / Support:  atjmh@asuvm.inre.asu.edu / After
installation, see help rnd.

rnd from http://www.stata.com/users/jhilbe
```
rnd. Random data generators. / Program by Joseph Hilbe, Arizona State Univ. <jhilbe@aol.com> / Statalist distribution, 29 January 1999. / These
```   programs generate random numbers for a variety of important /
```
distributions. This is an update of my STB-28 insert. / See help rnd.
```
rnd from http://fmwww.bc.edu/RePEc/bocode/r
```
'RND': modules for random number generation / The random number generators in this package are updated versions / of those published in STB-28 and
```   STB-44. They allow for / noncentrally distributed random numbers,
```
distributed F, t, or / Chi-squared. The other RNGs include some which are
```
```
- -rndlgn- was published in STB-28 and updated in STB-41. There is another copy on the StataCorp website. The most up-to-date public version is on SSC.
```
As I said, there has never been any such requirement of -rndlgn- to have
20000 or more observations. I don't know where such a bizarre idea comes
from. The help gives various examples of using -set obs- before you call
the -rnd- commands. Independently of that, the -rnd- programs will
increase the dataset size if the number of observations specified is
greater than the number of observations in memory. But the treatment of
dataset size has not changed at all in the public lifetime of -rndlgn-,
```
so there is no difference between older and newer versions of -rndlgn- in this respect.
```
The most notable change between STB-28 and SSC versions is a correction
```
of a typo in the comment: the third argument is the standard deviation, not the variance.
```
```
*!version 1.0.0 1993 Joseph Hilbe, Walter Linde-Zwirble (sg44: STB-28) * Lognormal distribution random number generator [via normal distribution]
```* Example: rndlgn 1000 0 .5  [set obs 1000; 0 = mean; .5 = variance]

program define rndlgn
version 3.1
cap drop xlgn
qui     {
local cases `1'
set obs `cases'
mac shift
local mn `1'
mac shift
local var `1'
mac shift
tempvar ran1
noi di in gr "( Generating " _c
gen `ran1' = exp(`mn'+`var' * invnorm(uniform()))
gen xlgn = `ran1'
noi di in gr "." _c
noi di in gr " )"
noi di in bl "Variable " in ye "xlgn " in bl "created."
}
end

*!version 1.1 1999 Joseph Hilbe
```
* version 1.0.0 1993 Joseph Hilbe (sg44: STB-28) * Lognormal distribution random number generator [via normal distribution] * Example: rndlgn 1000 0 .5 [set obs 1000; 0 = mean; .5 = std. deviation]
```
program define rndlgn
version 4.0
set type double
cap drop xlgn
qui     {
local cases `1'
set obs `cases'
mac shift
local mn `1'
mac shift
local var `1'
mac shift
tempvar ran1
noi di in gr "( Generating " _c
gen `ran1' = exp(`mn'+`var' * invnorm(uniform()))
gen xlgn = `ran1'
noi di in gr "." _c
noi di in gr " )"
noi di in bl "Variable " in ye "xlgn " in bl "created."
lab var xlgn "Lognormal random variable"
set type float
}
end

Second,  " For my case, I prefer to use rndlgn instead of exp(normal)
because I wanna [meaning:want to] specify heteroskedasticity."

There is no real difference here.

In Stata 10.1 up, you can call -rnormal()- with specified mean and
standard deviation; either or both could be given via variable names, or
indeed more complicated expressions. This is quite as flexible as,
indeed slightly more flexible than, using -rndlgn-, as the latter
reliably supports only single numbers or variable names as arguments.

Nick
n.j.cox@durham.ac.uk

sun samn

Sorry, I just updated my rndlgn; the old one does require 20000 or more.

For my case, I prefer to use rndlgn instead of exp(normal) because I
wanna specify heteroskedasticity.

n.j.cox@durham.ac.uk

```
```This sparked a thread with Martin Weiss. I have four comments by way
```
```of summarizing and going beyond that exchange.
```
```
```
1. Please do specify where user-written software you refer to comes
```from.
```
(Most postings from Sun Samn ignore advice in the FAQ in at least one
```way.)
```
```
2. It is quite untrue that -rndlgn- requires 20,000 observations.

3. In this case, the user-specified software is not, and never has
```
```been, needed. A one-line call with reference to -exp(rnormal())- gets
you random draws from a lognormal quite directly. (Before -rnormal()-
was introduced, other functions could be used, and indeed were used
internally within -rndlgn-. I imagine that for their own reasons Joe
Hilbe and friend wanted something uniform in syntax with the other
commands that they wrote a while back.)
```
```
```
4. -rndlgn- is a command, not a function. -exp()- and -rnormal()- are
```functions.

sun samn

```
```   I know the function ' rndlgn' can generate lognormal RVs, but it
```
```requires the numbers of observation to be at 20000. Now, I want a list
of only 500. What should I do then?

*

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```