Functions for using runif() and rnorm() with randtoolbox generators

Share:

Description

These functions allow to set some of the random number generators from randtoolbox package to be used instead of the standard generator in the functions, which use random numbers, for example runif(), rnorm(), sample() and set.seed().

Usage

1
2
3
4
5
6
set.generator(name=c("congruRand", "WELL", "MersenneTwister", "default"),
              parameters=NULL, seed=NULL, ..., only.dsc=FALSE)

put.description(description)		

get.description()

Arguments

name

A character string for the RNG name.

parameters

A numeric or character vector describing a specific RNG from the family specified by the name.

seed

A number, whose value is an integer between 0 and 2^32 - 1, to be used as a seed.

...

Arguments describing named components of the vector of parameters, if argument parameters is missing or NULL.

only.dsc

Logical. If TRUE, a description of the specified RNG is returned and the generator is not initialized.

description

A list describing a specific RNG as created by set.generator(,only.dsc=TRUE) or get.description().

Details

Random number generators provided by R extension packages are set using RNGkind("user-supplied"). The package randtoolbox assumes that this function is not called by the user directly and is called from the functions set.generator() and put.description().

Random number generators in randtoolbox are represented at the R level by a list containing mandatory components name, parameters, state and possibly an optional component authors. The function set.generator() internally creates this list from the user supplied information and then runs put.description() on this list, which initializes the generator. If the generator is initialized, then the function get.description() may be used to get the actual state of the generator, which may be stored in an R object and used later in put.description() to continue the sequence of the random numbers from the point, where get.description() was called. This may be used to generate several independent streams of random numbers generated by different generators.

The component parameters is a character or a numeric vector, whose structure is different for different types of the generators. This vector may be passed to set.generator(), if it is prepared before its call, however, it is also possible to pass its named components via the ... parametr of set.generator() and the vector parameters is created internally. If the vector parameters is not supplied and the arguments in ... are not sufficient to create it, an error message is produced.

Linear congruential generators

Parameters for the linear congruential generators (name="congruRand") are integers represented either as a character or a numeric vector. The components are

mod

The modulus.

mult

The multiplier.

incr

The increment.

WELL generators

Parameters for the WELL generators is a character vector with components

order

The number of bits in the internal state. Possible values are 512, 521, 607, 800, 1024, 19937, 21701, 23209, 44497.

version

The version letter "a", "b", or "c" to be appended to the order.

The concatenation of order and version should belong to "512a", "521a", "521b", "607a", "607b", "800a", "800b", "1024a", "1024b", "19937a", "19937b", "19937c", "21701a", "23209a", "23209b", "44497a", "44497b". When order and version are specified in ... parametr of set.generator(), then the parameter order is optional and if missing, it is assumed that the parameter version contains also the number of bits in the internal state and the combination belongs to the list above.

Mersenne Twister 2002 generator

Parameters for the Mersenne Twister 2002 is a character vector with components

initialization

Either "init2002" or "array2002". The initialization to be used.

resolution

Either 53 or 32. The number of random bits in each number.

Value

set.generator() with the parameter only.dsc=TRUE and get.description() return the list describing a generator. put.description() with the parameter only.dsc=TRUE (the default) and put.description() return NULL.

Author(s)

Petr Savicky and Christophe Dutang

See Also

RNGkind

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#set WELL19937a
set.generator("WELL", version="19937a", seed=12345)
runif(5)

#Store the current state  and generate 10 random numbers
storedState <- get.description()
x <- runif(10)

#Park Miller congruential generator
set.generator(name="congruRand", mod=2^31-1, mult=16807, incr=0, seed=12345)
runif(5)
setSeed(12345)
congruRand(5, dim=1, mod=2^31-1, mult=16807, incr=0)

# the Knuth Lewis RNG
set.generator(name="congruRand", mod="4294967296", mult="1664525", incr="1013904223", seed=1)
runif(5)
setSeed(1)
congruRand(5, dim=1, mod=4294967296, mult=1664525, incr=1013904223)

#Restore the generator from storedState and regenerate the same numbers
put.description(storedState)
x == runif(10)

# generate the same random numbers as in Matlab
set.generator("MersenneTwister", initialization="init2002", resolution=53, seed=12345)
runif(5)
# [1] 0.9296161 0.3163756 0.1839188 0.2045603 0.5677250
# Matlab commands rand('twister', 12345); rand(1, 5) generate the same numbers,
# which in short format are   0.9296    0.3164    0.1839    0.2046    0.5677

#Restore the original R setting
set.generator("default")
RNGkind()