runifInterface {randtoolbox} | R Documentation |
These functions allow to set some of the random number generators from randtoolbox
package so that it can be used in the standard R functions, which use random numbers,
for example runif()
, rnorm()
, sample()
and
also set.seed()
.
set.generator <- function(name=c("congruRand", "WELL", "default"), parameters=NULL, seed=NULL, ..., only.dsc=FALSE) put.description(description) get.description()
name |
a character string for the RNG name. |
parameters |
a numeric or character vector describing a RNG from the family
specified by the name parameter. |
seed |
a number to be used as a seed |
... |
arguments describing the components of the vector parameters,
if argument parameters is NULL. |
only.dsc |
a logical. If TRUE the description of a RNG is created,
but the generator is not initialized. |
description |
a list describing a generator as created by set.generator() |
or get.description()
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.
Instead, it is called from the functions set.generator()
and put.description()
used
for setting some of a larger collection of the supported generators.
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 in order to really initialize the generator for the functions runif()
and set.seed()
. If set.generator()
is called with the parameter
only.dsc=TRUE
, then the generator is not initialized and only
its description is created. 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 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, for example, to alternate between the streams of random numbers
generated by different generators.
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
.
Petr Savicky and Christophe Dutang
RNGkind
RNGkind() # [1] "Mersenne-Twister" "Inversion" #parameters for Park Miller congruential generator paramParkMiller <- c(mod=2^31-1, mult=16807, incr=0) set.generator(name="congruRand", parameters=paramParkMiller, seed=1) RNGkind() # [1] "user-supplied" "Inversion" #description of the RNG set by set.generator(), i.e. Park Miller print(ParkMiller <- get.description()) #generate 10 random points from the Park-Miller sequence x1 <- runif(10) #the seed has changed get.description() # the Knuth Lewis RNG paramKnuthLewis <- c(mod="4294967296", mult="1664525", incr="1013904223") set.generator(name="congruRand", parameters= paramKnuthLewis, seed=1) #description of the current RNG, i.e. Knuth Lewis KLwithseed1 <- get.description() x2 <- runif(10) #reinitiate the RNG setting put.description(ParkMiller) #the same as x1 x1 == runif(10) #set WELL RNGs set.generator("WELL", seed=12345, order=1024, version="a") get.description() #get back to the original R setting set.generator("default") RNGkind()