s-news
[Top] [All Lists]

Multiple streams of random numbers

To: s-news@lists.biostat.wustl.edu
Subject: Multiple streams of random numbers
From: Tim Hesterberg <timh@insightful.com>
Date: Wed, 13 Feb 2008 11:08:21 -0800
Dear S-news readers:

We wish to draw your attention to a problem with set.seed(),
this affects people who use multiple streams of random numbers.
The sequences obtained after
        set.seed(i1)
        set.seed(i2)
are not independent (details and a remedy below).

In general, it is best to use a single stream of numbers, rather
than multiple streams.  In other words, don't call set.seed routinely.
If you need to be able to reproduce a result, you can save
the value of .Random.seed.  To save the value in a form you can
paste into a script, use:
        dput(.Random.seed)

The dependencies between streams obtained using set.seed(i1) and
set.seed(i2) are strongest when i1-i2 is a multiple of a large power
of 2.  The worst case is:
        set.seed(0)
        x <- runif(1000)
        set.seed(512)
        y <- runif(1000)
The points fall in two stripes, rather than filling the unit square.
If seeds differ by 256, the points fall in four stripes.
More generally, if the seeds differ by a multiple of 2^c, the points
fall in 2^(10-c) stripes.

A remedy is to use the following function in place of set.seed:

setSeed <- function(k){
  # k is an integer, typically in 0:1023
  #
  # This is like set.seed(i = 7 * k, tausworthe = k)
  # (except set.seed ignores tausworthe if i is supplied)
  # This sets both the congruential and tausworthe seeds, in order
  # to prevent two subsequences from being dependent, which occurs
  # if only the congruential seed is set.
  if(length(k) != 1)
    stop("k must be length 1")
  k <- k %% 1024
  .C("setseed",
     as.integer(7 * k))
  .C("setseed_tausval",
     as.integer(k))
  invisible()
}

Another remedy is to call set.seed() with no arguments.  This sets the
seed to a random starting location.  However, if you use this
technique with multiple streams and use many random numbers, there is
a (small) chance the streams will overlap.

For additional information see 
http://www.insightful.com/insightful_faq/dsp_article.asp?articleID=33

Tim Hesterberg

========================================================
| Tim Hesterberg       Senior Research Scientist       |
| timh@insightful.com  Insightful Corp.                |
| (206)802-2319        1700 Westlake Ave. N, Suite 500 |
| (206)283-8691 (fax)  Seattle, WA 98109-3044, U.S.A.  |
|                      www.insightful.com/Hesterberg   |
========================================================
Download S+Resample from www.insightful.com/downloads/libraries

I'll teach short courses:
Advanced Programming in S-PLUS: San Antonio TX, March 26-27, 2008.
Bootstrap Methods and Permutation Tests: San Antonio, March 28, 2008.

<Prev in Thread] Current Thread [Next in Thread>
  • Multiple streams of random numbers, Tim Hesterberg <=