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]

RE: st: RE: Random start to random number sequence

From   "Lachenbruch, Peter" <>
To   "''" <>
Subject   RE: st: RE: Random start to random number sequence
Date   Thu, 19 Aug 2010 08:29:42 -0700

In using Bill's routine that gets the date and time, I found that it added 000 to the end and gave me numbers >2^31-1 which Stata didn't like.  I made a simple change to get
/* set seed for simulations */
local seed = tc(`c(current_date)' `c(current_time)')/1000
set seed `seed'
display "seed is " c(seed)

and all was well.  Perhaps I goofed in the local command (without the /1000)


Peter A. Lachenbruch
Department of Public Health
Oregon State University
Corvallis, OR 97330
Phone: 541-737-3832
FAX: 541-737-4001

-----Original Message-----
From: [] On Behalf Of William Gould, StataCorp LP
Sent: Thursday, August 19, 2010 8:10 AM
Subject: Re: st: RE: Random start to random number sequence

Clyde Schechter <> writes, 

> My approach to setting seeds [...]  I take out my wallet and
> examine the first currency note in it.  I strip off any letters
> from the serial number, and use that as the seed.  I put the note
> at the back of my wallet so I won't reuse that serial number.

Ooh, I like it.  I like it so much I'm going to use it.

For those without currency in their wallets, there are web sites that
will give you real random numbers.

Real Random numbers

One site is, which bases its numbers on
atmospheric noise.  Also see
for more information on the site.

Another is  They claim to to generate 
true random numbers using a quantum random-number generator.

I just found both the above sites, but I should have known about them.
My old favorite is back in the days when the
numbers were based on observations of a real lava lamp.  Nowadays,
Wikipedia informs me, numbers are based on noise from the
CCD of a cheap webcam, which doesn't have nearly the charm.  The
numbers are produced more quickly, however.  Anyway, provides some history.  To get a
seed from, go to the site and click on Lava Demos.  You'll
have a seed.

I'm sure others can find more sites, and I suspect that most Statalist 
members would prefer that we don't report all the sites we find.
If someone finds a site that is special, such as one that still uses 
a real lava lamp, or one that is particularly quick and easy to use, 
I suspect the other list members would tolerate it.  I'd like to hear 
about it.

For those interested in programming and performing a public service,
see the P.S. at the bottom.

You don't need real random numbers

Using real random-number generators for statistical simulations would
be overkill.  Even using real random-number generators for setting the
seed of Stata's generator is overkill, but unassailable.

These sites exist for cryptological reasons.  These people want real
random numbers not just because such numbers are appearently random,
but because they are truly unpredictable and thus cannot be reversed
engineered.  In our case, we don't care whether it would be possible
for a sophisticated person to take a sample of our simulation and
"break into" the rest of it.

Indeed, we desire the opposite.  We want our work to be easily
reproducible by us and by others.  Hence, Stata will display the state
of the random-number generator if you code -display "`c(seed)'"-, and
allow you to reset the generator back to that state later if you type
-set seed ...-, substituting what was displayed.

-- Bill

P.S.  A new command for Stata that reached out to the web and obtained 
      a real random number, set Stata's random number seed, displayed 
      the state of the random-number generator, and returned the 
      state in r() to boot, would be neat.  I'm imagining

		. setseed
                  (contacting http://www....)
                  (setting seed to 429839488)
                  (RNG state is now X52359df4c43f462544a474abacbdd93d2fba)

                . return list 
                    r(seed) : "X52359df4c43f462544a474abacbdd93d2fba"

      Programmers: If such a site existed, the trick to getting the random
      number would be

             tempfile fromweb
             copy "http://wwww..."; "`fromweb'"

      One could then process file "`fromweb'", possibly in Mata.

      To understand this method, try typing 

             . copy trythis.html

      That will create file trythis.html.  Take a look.

      If someone finds a site and undertakes this, please do not submit to SSC
      until someone else reviews your code.  This command would reach out to a
      foreign website.  Someone needs to verify that the site is on the up and
      up.  And somone needs to review your code to verify that your code will
      do nothing bad if the website changes the format of what it produces.

      I volunteer.

      In fact, the review is probably unnecessary even if the site is 
      evil and your code is poor.  That's because Stata is not a web 
      browser.  Pretend is an evil web site that will do 
      real damage to your computer if you just visited it in your 
      web browser.  Even so, in Stata, typing 

             . copy badfile 

      could not posibly do any damage to your computer because Stata
      does not interpret or even look at the contents being copied to
      the diskfile.  The danger would be that you might later open
      badfile in your browser and that could do real damage.  Reading
      badfile from Mata, however, would be safe because again, the
      evil code that is there is not interpreted.

      In any case, I recommend copying to a tempfile.  That way, no one 
      would even have time to open the file in a browser. 
      And anyway, we will check out that the site is a safe one.

*   For searches and help try:

*   For searches and help try:

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