Last data update: 2014.03.03

R: Implementation of Pierre L'Ecuyer's RngStreams
RNGstreamsR Documentation

Implementation of Pierre L'Ecuyer's RngStreams

Description

This is an R re-implementation of Pierre L'Ecuyer's ‘RngStreams’ multiple streams of pseudo-random numbers.

Usage

nextRNGStream(seed)
nextRNGSubStream(seed)

clusterSetRNGStream(cl = NULL, iseed)
mc.reset.stream()

Arguments

seed

An integer vector of length 7 as given by .Random.seed when the "L'Ecuyer-CMRG" RNG is in use. See RNG for the valid values.

cl

A cluster from this package or package snow, or (if NULL) the registered cluster.

iseed

An integer to be supplied to set.seed, or NULL not to set reproducible seeds.

Details

The ‘RngStream’ interface works with (potentially) multiple streams of pseudo-random numbers: this is particularly suitable for working with parallel computations since each task can be assigned a separate RNG stream.

This uses as its underlying generator RNGkind("L'Ecuyer-CMRG"), of L'Ecuyer (1999), which has a seed vector of 6 (signed) integers and a period of around 2^191. Each ‘stream’ is a subsequence of the period of length 2^127 which is in turn divided into ‘substreams’ of length 2^76.

The idea of L'Ecuyer et al (2002) is to use a separate stream for each of the parallel computations (which ensures that the random numbers generated never get into to sync) and the parallel computations can themselves use substreams if required. The original interface stores the original seed of the first stream, the original seed of the current stream and the current seed: this could be implemented in R, but it is as easy to work by saving the relevant values of .Random.seed: see the examples.

clusterSetRNGStream selects the "L'Ecuyer-CMRG" RNG and then distributes streams to the members of a cluster, optionally setting the seed of the streams by set.seed(iseed) (otherwise they are set from the current seed of the master process: after selecting the L'Ecuyer generator).

Calling mc.reset.stream() after setting the L'Ecuyer random number generator and seed makes runs from mcparallel(mc.set.seed = TRUE) reproducible. This is done internally in mclapply and pvec. (Note that it does not set the seed in the master process, so does not affect the fallback-to-serial versions of these functions.)

Value

For nextRNGStream and nextRNGSubStream, a value which can be assigned to .Random.seed.

Note

Interfaces to L'Ecuyer's C code are available in CRAN packages rlecuyer and rstream.

Author(s)

Brian Ripley

References

L'Ecuyer, P. (1999) Good parameters and implementations for combined multiple recursive random number generators. Operations Research 47, 159–164.

L'Ecuyer, P., Simard, R., Chen, E. J. and Kelton, W. D. (2002) An object-oriented random-number package with many long streams and substreams. Operations Research 50 1073–5.

See Also

RNG for fuller details of R's built-in random number generators.

The vignette for package parallel.

Examples

RNGkind("L'Ecuyer-CMRG")
set.seed(123)
(s <- .Random.seed)
## do some work involving random numbers.
nextRNGStream(s)
nextRNGSubStream(s)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(parallel)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/parallel/RngStream.Rd_%03d_medium.png", width=480, height=480)
> ### Name: RNGstreams
> ### Title: Implementation of Pierre L'Ecuyer's RngStreams
> ### Aliases: nextRNGStream nextRNGSubStream clusterSetRNGStream
> ###   mc.reset.stream
> ### Keywords: distribution sysdata
> 
> ### ** Examples
> 
> RNGkind("L'Ecuyer-CMRG")
> set.seed(123)
> (s <- .Random.seed)
[1]        407 1806547166 -983674937  643431772 1162448557 -959247990 -133913213
> ## do some work involving random numbers.
> nextRNGStream(s)
[1]         407  1801422725 -2057975723  1156894209  1595475487   210384600
[7] -1655729657
> nextRNGSubStream(s)
[1]         407   701401935  -355687987  1513178397  1735140456 -1584327974
[7]  -452417912
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>