Today I opened two STATA windows and ran the following command in both:

set obs 100
gen x = rnormal()
sort x

(the difference is that on the second window I generated a variable called y). Summing up: I asked STATA to give me 100 pseudo-random numbers taken from a standard normal distribution, then I sorted it. To my surprise, the numbers of the x and y vectors are the same! I did this at home, and then at work, and my impression is that all of these vectors are the same. Is there an explanation for this, to me, strange behavior?

If this is a problem in STATA, does R have a better pseudo-random number generator procedure?

A side question. I came up to this "problem" because I was trying to generate two pseudo-random columns in Stata (x and y, say), and then sort then separately. But the two commands I know for sorting (sort and gsort) sort the whole database, not separate columns. Would you know of a Stata command that allows me to sort a column while keeping the other columns fixed?

  • $\begingroup$ According to the manual for version 8, Stata uses the "KISS" generator by G. Marsaglia of period approximately 2^126. The only reason to need something better is for cryptographic applications. $\endgroup$
    – whuber
    May 24, 2011 at 16:12
  • 1
    $\begingroup$ Just to add that I tried the same thing in R and got the same behavior. Namely, I opened two R windows, ran the command x <- rnorm(50) and got the same vector of 50 random numbers in both windows. $\endgroup$
    – JJ O
    May 24, 2011 at 19:09

1 Answer 1


The help for set_seed states

The sequences these functions produce are determined by the seed, which is just a number and which is set to 123456789 every time Stata is launched.

Stata's philosophy emphasizes reproducibility, so this consistency is not surprising. Of course you can set the seed yourself. See the help page for more information.

One way to sort a column separately from all others is to preserve your data, keep only the column to sort, sort it, save the results in a temporary file, restore your data, and merge the temporary file:

gen y = rnormal()
keep y
sort y
tempfile out
save `out'
merge 1:1 _n using `out', nogen
  • $\begingroup$ Thanks Whuber - I read about the seed - I still have to digest the idea of how Stata treats randomness, but at least I now know that they are doing something right which is worth my time to understand. About the second part, I thought about the same approach (doing a merge), but it was great to see your code, as mine wouldn´t be so good! Cheers, and thanks a lot again. $\endgroup$
    – JJ O
    May 24, 2011 at 17:45
  • 2
    $\begingroup$ If you actually want the random numbers to change every time you start Stata: one idea would be to base you "seed" on your clock using your profile.do file. Try set seed `=clock("$S_DATE $S_TIME","DMYhms" )-162e10' $\endgroup$
    – Keith
    May 25, 2011 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.