Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Random start to random number sequence

From   "Nick Cox" <>
To   <>
Subject   st: RE: Random start to random number sequence
Date   Tue, 17 Aug 2010 18:32:29 +0100

Allan's code, if I understand it correctly, could have the odd
consequence that two or more sets of random numbers _generated within
the same second on the same machine_ would be identical. That is, if you
are looping around and getting fresh sets of random numbers each time,
as one often does, successive sets might be identical. 

(The words "on the same machine" are almost redundant. I just put them
in to emphasise that I don't care about random numbers on someone else's
machine. On the other hand, that could bite if you were working with
several Statas and combining the results.) 

That is not that far-fetched, and not that desirable. 

On the other hand, if this seed setting is done just once, that won't

I always had a sense that while reproducibility through using the same
seed is often important, the whole art of simulation otherwise is to
make enough checks -- through taking enough samples, through
scrutinising results -- to ensure that choice of seed is utterly
immaterial. So being fastidious about choice of seed is more a matter of
public appearances than anything else. Using the system clock sounds
more "objective", even "professional", but would using your own birthday
really be that different? 


Allan Reese (Cefas)

I looked on Statalist for a simple function to start the random number
generator with a random value and found lots of rather confusing
messages.  The Manual stresses the ability to repeat sequences from one
session to another, rather than to generate an (almost certainly for
practical purposes) unique sequence.  

The uniform() function to generate (pseudo) random numbers starts from
the same value in each Stata session unless you change the start value
with "set seed -value-", where value is an integer ([D] gen).  This is
very useful in allowing simulations to be repeatable but open to
variation: you get very different random sequences by setting the seed
to 1, 2, 3, etc.

On the other hand, you may simply require a different random sequence in
each session.  Rather than remember the seed values you have used
previously, you can use the system clock which changes every second.
This will not make the subsequent sequence any more (or less) random,
but will make each session unique.  The "code" (see -help seed- to
explain) can be displayed or saved in case you do want to reproduce a

Using the clock has been suggested several time but with a complex
parsing.  Here is a simple program that can be added to your ado

program define randomseed
   version 7
   local seed = clock( c(current_time), "hms" )
   set seed  `seed'
   di "Clock based seed = " c(seed)

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index