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: Random start to random number sequence

From   "Allan Reese (Cefas)" <>
To   <>
Subject   st: Random start to random number sequence
Date   Tue, 17 Aug 2010 10:02:22 +0100

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)

R Allan Reese
Senior statistician, Cefas
The Nothe, Weymouth DT4 8UB 

Tel: +44 (0)1305 206614 -direct
Fax: +44 (0)1305 206601 

This email and any attachments are intended for the named recipient only.  Its unauthorised use, distribution, disclosure, storage or copying is not permitted.  If you have received it in error, please destroy all copies and notify the sender.  In messages of a non-business nature, the views and opinions expressed are the author's own and do not necessarily reflect those of the organisation from which it is sent.  All emails may be subject to monitoring.

*   For searches and help try:

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